python写的一个合并表格工具,可以把文件夹内多个表格按行堆叠合并一个新的表格,简单好用。代码结尾附打包exe命令。
代码截图
软件截图
[Python] 纯文本查看 复制代码 import os
import tkinter as tk
from tkinter import filedialog
from openpyxl import load_workbook, Workbook
# 定义选择文件夹的函数
def select_folder(entry):
folder_path = filedialog.askdirectory()
if folder_path:
entry.delete(0, tk.END)
entry.insert(0, folder_path)
# 定义合并Excel文件的函数
def merge_excel():
folder_path = entry1.get()
if folder_path:
# 创建一个新的Workbook
wb_merged = Workbook()
ws_merged = wb_merged.active
# 遍历文件夹中的所有Excel文件
for filename in os.listdir(folder_path):
if filename.endswith('.xlsx') or filename.endswith('.xls'):
file_path = os.path.join(folder_path, filename)
wb = load_workbook(file_path)
# 复制工作簿中的所有工作表的所有行
for ws in wb.worksheets:
for row in ws.iter_rows(values_only=True):
ws_merged.append(row)
output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel files", "*.xlsx")])
if output_file_path:
wb_merged.save(output_file_path)
result_label.config(text="文件合并成功!")
else:
result_label.config(text="已取消保存。")
else:
result_label.config(text="请选择文件夹。")
# 创建主窗口
root = tk.Tk()
root.title("52pj专用合并表格工具")
# 创建框架
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
# 创建并放置小部件
label1 = tk.Label(frame, text="选择包含Excel文件的文件夹:")
label1.grid(row=0, column=0, sticky="w")
entry1 = tk.Entry(frame, width=50)
entry1.grid(row=0, column=1)
button1 = tk.Button(frame, text="浏览", command=lambda: select_folder(entry1))
button1.grid(row=0, column=2)
merge_button = tk.Button(frame, text="合并Excel文件", command=merge_excel)
merge_button.grid(row=1, column=0, columnspan=3)
result_label = tk.Label(frame, text="")
result_label.grid(row=2, column=0, columnspan=3)
# 启动主循环
root.mainloop()
# 终端执行打包exe命令(pyinstaller -F -w 文件夹表格合并.py)
|