Excel文件比较器
25-04-08 02:31
1029
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()
-
个人发卡网全开源解密版 集成了码支付和轻云支付接口+安装教程
这款发卡网源码已经集成了码支付和轻云支付接口,并且有4个前台样式模板,当然如果你都不满意,可以自己写样式,毕竟这款发卡平台源码无加... 397 0 21-07-02 -
仿微信app微信页面html
495 0 21-06-04 -
聊天表情包
581 0 21-06-02 -
国家数据 - 国家统计数据
国家统计局是中国政府的官方机构,负责收集、处理和发布国家统计数据。您可以在此找到包括人口、经济发展、教育、农业等各个领域的官方数据... 880 0 25-01-08 -
常用科学方法归纳
学术界对于科学的定义,至今没有一个大家公认的简明定义和说法,即使在不同版本的词典和辞书中,科学的定义也是公说公婆说婆。其原... 810 0 25-03-03 -
设计足够可靠的分布式缓存体系,以满足大中型移动互联网系统的需要
传统 CAP 的突破随着分布式系统的不断演进,会不断遇到各种问题,特别是当前,在大中型互联网系统的演进中,私有云、公有云并行发展且相... 1021 0 24-05-24 -
PCMaster(软媒魔方电脑大师)_v6.25_去广告纯净版
软媒魔方6支持64位和32位的所有主流Windows系统,从优化大师发展为一款系统增强套装,自动化、智能化解决各种电脑问题。软媒魔方内置20余款... 848 0 25-04-06 -
奈特病理学彩色图谱
概述病理解剖学是鉴定和解释活体组织形态结构及相关生理和病理功能的一门科学,包括大体和镜下两部分。病理学帮助我们阐述疾病发病机制和确... 1164 0 24-06-18
发表我的评论
共0条评论
- 这篇文章还没有收到评论,赶紧来抢沙发吧~