基于指定目录文本文件的Visual Basic采购订单报表生成方法咨询
Got it, let's walk through how to build this purchase order report generator step by step. I'll use Python with Tkinter for the GUI since it's straightforward for desktop tools, but the core logic translates to other languages too.
First, let's clarify exactly what we need to build:
- Read all target text files from a specified directory
- Extract site information from each purchase order entry in the files
- Calculate:
- Total number of purchase orders
- Count of orders sent to Site1 (labeled "实验室")
- Count of orders sent to Site2 (labeled "标签")
- Count of orders sent to Site3 (labeled "标签")
- Build a GUI with a button to trigger the report, and read-only text boxes to display the results
Since you didn't specify the file structure, I'll use a common comma-separated format for example. Adjust this based on your actual file layout:
Example text file content (each line represents one purchase order):
PO-001,Site1,2024-05-20
PO-002,Site2,2024-05-21
PO-003,Site1,2024-05-22
PO-004,Site3,2024-05-23
3.1 Import Required Libraries
import os import tkinter as tk from tkinter import filedialog, messagebox
3.2 Write the Report Generation Function
This function handles directory selection, file reading, data counting, and updating the GUI:
def generate_report(): # Let user select the target directory target_dir = filedialog.askdirectory(title="选择包含采购订单文件的目录") if not target_dir: return # Exit if user cancels selection # Initialize counters total_pos = 0 site_counts = { "Site1": 0, # 实验室 "Site2": 0, # 标签 "Site3": 0 # 标签 } # Iterate through all text files in the directory for filename in os.listdir(target_dir): if filename.endswith(".txt"): file_path = os.path.join(target_dir, filename) try: with open(file_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line: continue # Skip empty lines # Extract site (adjust this based on your actual file format) parts = line.split(',') if len(parts) >= 2: site = parts[1].strip() total_pos += 1 if site in site_counts: site_counts[site] += 1 except Exception as e: messagebox.showerror("读取错误", f"处理文件 {filename} 时出错: {str(e)}") # Update the GUI text boxes with results total_var.set(total_pos) site1_var.set(site_counts["Site1"]) site2_var.set(site_counts["Site2"]) site3_var.set(site_counts["Site3"])
3.3 Build the GUI Interface
# Create main window root = tk.Tk() root.title("采购订单报表生成器") root.geometry("450x280") # Define variables to bind with text boxes total_var = tk.StringVar(value="0") site1_var = tk.StringVar(value="0") site2_var = tk.StringVar(value="0") site3_var = tk.StringVar(value="0") # Layout the widgets # Total PO count tk.Label(root, text="采购订单总数:").grid(row=0, column=0, padx=15, pady=12, sticky="w") tk.Entry(root, textvariable=total_var, state="readonly", width=20).grid(row=0, column=1, padx=15, pady=12) # Site1 (实验室) count tk.Label(root, text="发往实验室(Site1)的订单数:").grid(row=1, column=0, padx=15, pady=12, sticky="w") tk.Entry(root, textvariable=site1_var, state="readonly", width=20).grid(row=1, column=1, padx=15, pady=12) # Site2 (标签) count tk.Label(root, text="发往标签(Site2)的订单数:").grid(row=2, column=0, padx=15, pady=12, sticky="w") tk.Entry(root, textvariable=site2_var, state="readonly", width=20).grid(row=2, column=1, padx=15, pady=12) # Site3 (标签) count tk.Label(root, text="发往标签(Site3)的订单数:").grid(row=3, column=0, padx=15, pady=12, sticky="w") tk.Entry(root, textvariable=site3_var, state="readonly", width=20).grid(row=3, column=1, padx=15, pady=12) # Report button tk.Button(root, text="生成报表", command=generate_report, width=18, height=1).grid(row=4, column=0, columnspan=2, pady=20) # Run the main GUI loop root.mainloop()
- Adjust file format parsing: If your files use a different structure (e.g., space-separated, or have keywords like
Site: Site1), modify the extraction logic. For example, using regex for keyword-based sites:import re match = re.search(r'Site:\s*(\w+)', line) if match: site = match.group(1) - Filter specific files: If you only want to process files with names like
PO-*.txt, use thefnmatchlibrary:import fnmatch if fnmatch.fnmatch(filename, "PO-*.txt"): # Process the file - Fix encoding issues: If your files use GBK or other encodings, replace
encoding='utf-8'withencoding='gbk'in theopen()call.
- Install Python if you haven't already
- Save the code as
po_report_generator.py - Run the script, click "生成报表", select your target directory, and the results will populate automatically
内容的提问来源于stack exchange,提问作者M.Cuardal

