Excel文件比较器
25-04-08
slbcun
952℃
0
import tkinter as tk from tkinter import ttk, filedialog, messagebox import pandas as pd import os from datetime import datetime class ExcelComparator: """ Excel文件比较器GUI应用程序 """ def __init__(self, root): self.root = root self.root.title("Excel文件比较器") self.root.geometry("800x700") # 文件路径变量 self.file_a_path = tk.StringVar() self.file_b_path = tk.StringVar() # 列选择变量 self.selected_column = tk.StringVar() # 存储比较结果 self.comparison_result = None self.setup_ui() def setup_ui(self): """ 设置GUI界面 """ # 软件说明 description_frame = ttk.LabelFrame(self.root, text="软件说明", padding="10") description_frame.pack(fill="x", padx=10, pady=5) description_text = """ 本软件用于比较两个Excel文件中的数据差异: 1. 选择两个Excel文件(A文件和B文件) 2. 选择要比对的列 3. 点击"开始比较"按钮 4. 软件会找出A文件中独有的数据(即A中有而B中没有的数据) 5. 可以点击"导出Excel"将结果保存到新的Excel文件中 使用场景: - 数据核对 - 数据查重 - 数据筛选 - 数据对比分析 """ description_label = ttk.Label(description_frame, text=description_text, justify="left") description_label.pack(fill="x", padx=5, pady=5) # 文件A选择 frame_a = ttk.LabelFrame(self.root, text="文件A(源文件)", padding="10") frame_a.pack(fill="x", padx=10, pady=5) ttk.Entry(frame_a, textvariable=self.file_a_path, width=50).pack(side="left", padx=5) ttk.Button(frame_a, text="选择文件", command=lambda: self.select_file("a")).pack(side="left", padx=5) # 文件B选择 frame_b = ttk.LabelFrame(self.root, text="文件B(对比文件)", padding="10") frame_b.pack(fill="x", padx=10, pady=5) ttk.Entry(frame_b, textvariable=self.file_b_path, width=50).pack(side="left", padx=5) ttk.Button(frame_b, text="选择文件", command=lambda: self.select_file("b")).pack(side="left", padx=5) # 列选择 frame_column = ttk.LabelFrame(self.root, text="选择比对列", padding="10") frame_column.pack(fill="x", padx=10, pady=5) column_label = ttk.Label(frame_column, text="请选择要比对的列(将根据此列的值进行比对):") column_label.pack(fill="x", padx=5, pady=2) self.column_combo = ttk.Combobox(frame_column, textvariable=self.selected_column) self.column_combo.pack(fill="x", padx=5, pady=2) # 按钮框架 button_frame = ttk.Frame(self.root) button_frame.pack(pady=10) # 比较按钮 ttk.Button(button_frame, text="开始比较", command=self.compare_files).pack(side="left", padx=5) # 导出按钮 self.export_button = ttk.Button(button_frame, text="导出Excel", command=self.export_excel, state="disabled") self.export_button.pack(side="left", padx=5) # 结果显示区域 result_frame = ttk.LabelFrame(self.root, text="比较结果", padding="10") result_frame.pack(fill="both", expand=True, padx=10, pady=5) self.result_text = tk.Text(result_frame, height=15, width=80) self.result_text.pack(fill="both", expand=True, padx=5, pady=5) # 添加滚动条 scrollbar = ttk.Scrollbar(result_frame, orient="vertical", command=self.result_text.yview) scrollbar.pack(side="right", fill="y") self.result_text.configure(yscrollcommand=scrollbar.set) def select_file(self, file_type): """ 选择Excel文件 Args: file_type (str): 文件类型标识('a'或'b') """ file_path = filedialog.askopenfilename( filetypes=[("Excel files", "*.xlsx *.xls")] ) if file_path: if file_type == "a": self.file_a_path.set(file_path) self.update_columns(file_path) else: self.file_b_path.set(file_path) def update_columns(self, file_path): """ 更新列选择下拉框 Args: file_path (str): Excel文件路径 """ try: df = pd.read_excel(file_path) self.column_combo['values'] = list(df.columns) if len(df.columns) > 0: self.column_combo.set(df.columns[0]) except Exception as e: messagebox.showerror("错误", f"读取文件失败:{str(e)}") def compare_files(self): """ 比较两个Excel文件并显示结果 """ if not self.file_a_path.get() or not self.file_b_path.get(): messagebox.showerror("错误", "请选择两个Excel文件") return if not self.selected_column.get(): messagebox.showerror("错误", "请选择比对列") return try: # 读取Excel文件 df_a = pd.read_excel(self.file_a_path.get()) df_b = pd.read_excel(self.file_b_path.get()) # 获取选择的列 column = self.selected_column.get() # 检查列是否存在 if column not in df_a.columns or column not in df_b.columns: messagebox.showerror("错误", "选择的列在其中一个文件中不存在") return # 找出A中不在B中的数据 self.comparison_result = df_a[~df_a[column].isin(df_b[column])] # 显示结果 self.result_text.delete(1.0, tk.END) if len(self.comparison_result) == 0: self.result_text.insert(tk.END, "没有找到A中独有的数据") self.export_button.config(state="disabled") else: self.result_text.insert(tk.END, f"找到{len(self.comparison_result)}条A中独有的数据:\n\n") self.result_text.insert(tk.END, self.comparison_result.to_string()) self.export_button.config(state="normal") except Exception as e: messagebox.showerror("错误", f"比较过程中出错:{str(e)}") def export_excel(self): """ 导出比较结果到Excel文件 """ if self.comparison_result is None or len(self.comparison_result) == 0: messagebox.showwarning("警告", "没有可导出的数据") return try: # 生成默认文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") default_filename = f"比较结果_{timestamp}.xlsx" # 选择保存路径 file_path = filedialog.asksaveasfilename( defaultextension=".xlsx", filetypes=[("Excel files", "*.xlsx")], initialfile=default_filename ) if file_path: # 导出到Excel self.comparison_result.to_excel(file_path, index=False) messagebox.showinfo("成功", "数据已成功导出到Excel文件!") except Exception as e: messagebox.showerror("错误", f"导出Excel时出错:{str(e)}") if __name__ == "__main__": root = tk.Tk() app = ExcelComparator(root) root.mainloop()