1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| import tkinter as tk from tkinter import ttk, filedialog, messagebox import os import sys import time
sys.path.append(os.path.dirname(os.path.abspath(__file__))) from excel_beautifier import ExcelBeautifier from merge_and_beautify import merge_excel_files, merge_and_beautify
class ExcelBeautifierGUI: def __init__(self, root): """初始化GUI界面""" self.root = root self.root.title("Excel美化工具 v1.1") self.root.geometry("850x650") self.root.resizable(width=False, height=False) try: self.beautifier = ExcelBeautifier() except Exception as e: messagebox.showerror("初始化失败", f"配置文件加载出错:{str(e)}") self.root.quit() return self.mode_var = tk.StringVar(value="single") self._create_main_frame()
def _create_main_frame(self): """创建主界面""" mode_frame = ttk.LabelFrame(self.root, text="🔧 操作模式选择") mode_frame.pack(fill="x", padx=15, pady=8) ttk.Radiobutton(mode_frame, text="单文件美化", variable=self.mode_var, value="single", command=self._switch_mode).pack(side="left", padx=10, pady=5) ttk.Radiobutton(mode_frame, text="批量文件夹美化", variable=self.mode_var, value="batch", command=self._switch_mode).pack(side="left", padx=10, pady=5) ttk.Radiobutton(mode_frame, text="仅合并文件", variable=self.mode_var, value="merge", command=self._switch_mode).pack(side="left", padx=10, pady=5) ttk.Radiobutton(mode_frame, text="合并+美化", variable=self.mode_var, value="merge_beautify", command=self._switch_mode).pack(side="left", padx=10, pady=5) self.io_frame = ttk.LabelFrame(self.root, text="📁 文件/文件夹选择") self.io_frame.pack(fill="x", padx=15, pady=8) self.single_input = tk.StringVar() self.single_output = tk.StringVar() self.batch_input = tk.StringVar() self.batch_output = tk.StringVar() self.merge_output = tk.StringVar() self._show_single_mode() btn_frame = ttk.Frame(self.root) btn_frame.pack(pady=10) ttk.Button(btn_frame, text="▶️ 开始执行", command=self._execute, width=15).pack(side="left", padx=10) ttk.Button(btn_frame, text="🗑️ 清空日志", command=self._clear_log, width=15).pack(side="left", padx=10) log_frame = ttk.LabelFrame(self.root, text="📋 执行日志") log_frame.pack(fill="both", expand=True, padx=15, pady=8) self.log_text = tk.Text(log_frame, font=("Consolas", 9), wrap=tk.WORD) scrollbar = ttk.Scrollbar(log_frame, orient="vertical", command=self.log_text.yview) self.log_text.configure(yscrollcommand=scrollbar.set) self.log_text.pack(side="left", fill="both", expand=True, padx=(0, 5), pady=5) scrollbar.pack(side="right", fill="y", pady=5) self._log("✅ 工具初始化完成,选择操作模式后开始执行")
def _show_single_mode(self): """显示单文件模式""" for widget in self.io_frame.winfo_children(): widget.destroy() ttk.Label(self.io_frame, text="输入Excel文件:").grid(row=0, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.single_input, width=50).grid(row=0, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="选择文件", command=lambda: self._select_file(self.single_input), width=10).grid(row=0, column=2, padx=5, pady=8) ttk.Label(self.io_frame, text="输出Excel文件:").grid(row=1, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.single_output, width=50).grid(row=1, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="保存位置", command=lambda: self._save_file(self.single_output), width=10).grid(row=1, column=2, padx=5, pady=8)
def _show_batch_mode(self): """显示批量模式""" for widget in self.io_frame.winfo_children(): widget.destroy() ttk.Label(self.io_frame, text="输入文件夹:").grid(row=0, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.batch_input, width=50).grid(row=0, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="选择文件夹", command=lambda: self._select_dir(self.batch_input), width=10).grid(row=0, column=2, padx=5, pady=8) ttk.Label(self.io_frame, text="输出文件夹:").grid(row=1, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.batch_output, width=50).grid(row=1, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="选择文件夹", command=lambda: self._select_dir(self.batch_output), width=10).grid(row=1, column=2, padx=5, pady=8)
def _show_merge_mode(self): """显示合并模式(仅合并/合并+美化)""" for widget in self.io_frame.winfo_children(): widget.destroy() ttk.Label(self.io_frame, text="输入文件夹:").grid(row=0, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.batch_input, width=50).grid(row=0, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="选择文件夹", command=lambda: self._select_dir(self.batch_input), width=10).grid(row=0, column=2, padx=5, pady=8) ttk.Label(self.io_frame, text="输出Excel文件:").grid(row=1, column=0, padx=5, pady=8, sticky="w") ttk.Entry(self.io_frame, textvariable=self.merge_output, width=50).grid(row=1, column=1, padx=5, pady=8) ttk.Button(self.io_frame, text="保存位置", command=lambda: self._save_file(self.merge_output), width=10).grid(row=1, column=2, padx=5, pady=8)
def _switch_mode(self): """切换操作模式""" mode = self.mode_var.get() if mode == "single": self._show_single_mode() self._log("🔄 切换到【单文件美化】模式") elif mode == "batch": self._show_batch_mode() self._log("🔄 切换到【批量文件夹美化】模式") elif mode in ["merge", "merge_beautify"]: self._show_merge_mode() self._log(f"🔄 切换到【{'仅合并文件' if mode=='merge' else '合并+美化'}】模式")
def _select_file(self, var): """选择文件""" file_path = filedialog.askopenfilename(title="选择Excel文件", filetypes=[("Excel文件", "*.xlsx *.xls"), ("所有文件", "*.*")]) if file_path: var.set(file_path)
def _save_file(self, var): """选择保存文件路径""" file_path = filedialog.asksaveasfilename(title="保存Excel文件", defaultextension=".xlsx", filetypes=[("Excel文件", "*.xlsx"), ("所有文件", "*.*")]) if file_path: var.set(file_path)
def _select_dir(self, var): """选择文件夹""" dir_path = filedialog.askdirectory(title="选择文件夹") if dir_path: var.set(dir_path)
def _log(self, msg): """输出日志""" timestamp = time.strftime("[%Y-%m-%d %H:%M:%S]") self.log_text.insert(tk.END, f"{timestamp} {msg}\n") self.log_text.see(tk.END) self.root.update_idletasks()
def _clear_log(self): """清空日志""" self.log_text.delete(1.0, tk.END) self._log("✅ 日志已清空")
def _execute(self): """执行核心操作""" mode = self.mode_var.get() self._log("="*50) try: if mode == "single": input_path = self.single_input.get().strip() output_path = self.single_output.get().strip() if not input_path or not output_path: raise ValueError("请选择输入文件和输出文件路径") if not os.path.exists(input_path): raise FileNotFoundError(f"输入文件不存在:{input_path}") self._log(f"🚀 开始美化单文件:{input_path}") success, msg = self.beautifier.beautify_single_file(input_path, output_path) self._log(f"📝 执行结果:{msg}") messagebox.showinfo("成功" if success else "失败", msg) elif mode == "batch": input_dir = self.batch_input.get().strip() output_dir = self.batch_output.get().strip() if not input_dir or not output_dir: raise ValueError("请选择输入文件夹和输出文件夹路径") if not os.path.exists(input_dir): raise FileNotFoundError(f"输入文件夹不存在:{input_dir}") self._log(f"🚀 开始批量美化文件夹:{input_dir}") success, msg = self.beautifier.beautify_batch_files(input_dir, output_dir) self._log(f"📝 执行结果:\n{msg}") messagebox.showinfo("成功" if success else "失败", msg) elif mode == "merge": input_dir = self.batch_input.get().strip() output_path = self.merge_output.get().strip() if not input_dir or not output_path: raise ValueError("请选择输入文件夹和输出文件路径") if not os.path.exists(input_dir): raise FileNotFoundError(f"输入文件夹不存在:{input_dir}") self._log(f"🚀 开始仅合并文件:{input_dir}") merge_success = merge_excel_files(input_dir, output_path) if merge_success: self._log(f"✅ 仅合并完成!输出文件:{output_path}") messagebox.showinfo("成功", "文件合并完成!") else: self._log("❌ 合并失败!") messagebox.showerror("失败", "文件合并失败,请查看日志") elif mode == "merge_beautify": input_dir = self.batch_input.get().strip() output_path = self.merge_output.get().strip() if not input_dir or not output_path: raise ValueError("请选择输入文件夹和输出文件路径") if not os.path.exists(input_dir): raise FileNotFoundError(f"输入文件夹不存在:{input_dir}") temp_file = os.path.join(os.path.dirname(output_path), "temp_merge.xlsx") self._log(f"🚀 开始合并+美化:{input_dir}") success = merge_and_beautify(input_dir, temp_file, output_path) if os.path.exists(temp_file): try: os.remove(temp_file) self._log(f"🗑️ 删除临时文件:{temp_file}") except: self._log(f"⚠️ 临时文件 {temp_file} 被占用,未删除") if success: self._log(f"✅ 合并+美化完成!输出文件:{output_path}") messagebox.showinfo("成功", "合并+美化完成!") else: self._log("❌ 合并+美化失败!") messagebox.showerror("失败", "合并+美化失败,请查看日志") except Exception as e: error_msg = f"❌ 执行出错:{str(e)}" self._log(error_msg) messagebox.showerror("执行错误", error_msg)
if __name__ == "__main__": root = tk.Tk() root.option_add("*Font", "SimHei 10") app = ExcelBeautifierGUI(root) root.mainloop()
|