[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from splitExcel import splitExcel, saveWorksheetToWorkbook # 假设你的原脚本名为 splitExcel.py
class ExcelSplitterGUI:
def __init__(self, root):
self.root = root
self.root.title("Excel 拆分工具")
self.root.geometry("550x500")
self.root.resizable(False, False)
# 设置主题样式
self.style = ttk.Style()
self.style.theme_use('clam')
# 配置样式
self.style.configure('Title.TLabel', font=('Arial', 12, 'bold'), foreground='#2c3e50')
self.style.configure('Custom.TButton', font=('Arial', 10), background='#3498db', foreground='white')
self.style.configure('Success.TButton', font=('Arial', 10, 'bold'), background='#27ae60', foreground='white')
self.style.configure('TEntry', padding=5)
# 初始化变量
self.file_path = tk.StringVar()
self.title_line = tk.IntVar(value=2)
self.split_column = tk.IntVar(value=2)
self.create_widgets()
def create_widgets(self):
# 主框架
main_frame = ttk.Frame(self.root, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# 标题
title_label = ttk.Label(main_frame, text="Excel 文件拆分工具", style='Title.TLabel')
title_label.pack(pady=(0, 20))
# 文件选择区域
file_frame = ttk.LabelFrame(main_frame, text="文件选择", padding=10)
file_frame.pack(fill=tk.X, pady=5)
ttk.Label(file_frame, text="Excel 文件路径:").grid(row=0, column=0, sticky=tk.W, pady=5)
file_entry = ttk.Entry(file_frame, textvariable=self.file_path, width=50)
file_entry.grid(row=1, column=0, sticky=tk.W+tk.E, pady=5)
browse_btn = ttk.Button(file_frame, text="浏览文件", command=self.select_file, style='Custom.TButton')
browse_btn.grid(row=1, column=1, padx=(10, 0), pady=5)
file_frame.columnconfigure(0, weight=1)
# 参数设置区域
params_frame = ttk.LabelFrame(main_frame, text="拆分参数", padding=10)
params_frame.pack(fill=tk.X, pady=10)
# 标题行设置
ttk.Label(params_frame, text="标题行号(从1开始):").grid(row=0, column=0, sticky=tk.W, pady=5)
title_spinbox = ttk.Spinbox(params_frame, from_=1, to=100, textvariable=self.title_line, width=10)
title_spinbox.grid(row=0, column=1, sticky=tk.W, pady=5, padx=(10, 0))
# 拆分列设置
ttk.Label(params_frame, text="拆分列号(从1开始):").grid(row=1, column=0, sticky=tk.W, pady=5)
split_spinbox = ttk.Spinbox(params_frame, from_=1, to=100, textvariable=self.split_column, width=10)
split_spinbox.grid(row=1, column=1, sticky=tk.W, pady=5, padx=(10, 0))
# 开始按钮
start_btn = ttk.Button(main_frame, text="开始拆分", command=self.run_split, style='Success.TButton')
start_btn.pack(pady=20)
# 日志输出区域
log_frame = ttk.LabelFrame(main_frame, text="执行日志", padding=10)
log_frame.pack(fill=tk.BOTH, expand=True, pady=5)
# 添加滚动条
log_scrollbar = ttk.Scrollbar(log_frame)
log_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.log_text = tk.Text(
log_frame,
height=10,
width=60,
state='disabled',
yscrollcommand=log_scrollbar.set,
bg='#f8f9fa',
fg='#2c3e50',
font=('Consolas', 9),
padx=10,
pady=10
)
self.log_text.pack(fill=tk.BOTH, expand=True)
log_scrollbar.config(command=self.log_text.yview)
# 状态栏
self.status_var = tk.StringVar(value="就绪")
status_bar = ttk.Label(main_frame, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W)
status_bar.pack(fill=tk.X, pady=(10, 0))
def select_file(self):
file_path = filedialog.askopenfilename(
title="选择Excel文件",
filetypes=[("Excel files", "*.xlsx *.xls"), ("All files", "*.*")]
)
if file_path:
self.file_path.set(file_path)
self.log(f"已选择文件: {file_path}")
def log(self, message):
self.log_text.config(state='normal')
self.log_text.insert(tk.END, f"{message}\n")
self.log_text.config(state='disabled')
self.log_text.see(tk.END)
self.root.update_idletasks()
def run_split(self):
file_path = self.file_path.get()
if not file_path:
messagebox.showwarning("警告", "请选择一个 Excel 文件!")
return
title_line = self.title_line.get()
split_column = self.split_column.get()
try:
self.status_var.set("正在处理...")
self.log("=" * 50)
self.log("开始拆分Excel文件")
self.log("=" * 50)
self.log("开始初始化...")
splitter = splitExcel(file_path, title_line, split_column)
self.log("选择工作表...")
splitter.selectSplitSheet()
self.log("读取数据...")
splitter.readData()
self.log("读取格式...")
splitter.readCellsStyle()
self.log("写入数据...")
splitter.writeDataToNewWorkbook()
self.log("写入格式...")
splitter.writeFormatToNewWorkbook()
self.log("保存文件...")
output_file = splitter.save()
self.log(f"✓ 拆分汇总文件已保存至:{output_file}")
self.log("正在保存每个子表为独立文件...")
saver = saveWorksheetToWorkbook(output_file)
folder = saver.saveTo(savePath=None)
self.log(f"✓ 拆分表保存文件夹:{folder}")
self.log("=" * 50)
self.log("✓ 拆分任务已完成!")
self.log("=" * 50)
self.status_var.set("任务完成")
messagebox.showinfo("完成", "拆分任务已完成!")
except Exception as e:
self.status_var.set("发生错误")
messagebox.showerror("错误", str(e))
self.log(f"✗ 发生错误:{str(e)}")
self.log("=" * 50)
self.log("✗ 任务失败")
self.log("=" * 50)
if __name__ == "__main__":
root = tk.Tk()
app = ExcelSplitterGUI(root)
root.mainloop()