当前位置:首页>python>Python入门连载(八)带窗口的可视化小工具

Python入门连载(八)带窗口的可视化小工具

  • 2026-10-11 08:37:40
Python入门连载(八)带窗口的可视化小工具

Python入门连载(八)带窗口的可视化小工具

一、这个工具能实现什么?
有窗口界面,不是黑乎乎的终端
点一下按钮,自动合并多个 Excel
自动选择文件夹,不用手写路径
发给同事、领导都能直接用
不暴露代码,安全又专业

二、准备工作
我们用 Python 最简单的 GUI 库:tkinter(系统自带,不用装)
再加上之前的 pandas:
pip install pandas openpyxl

三、完整可视化小工具代码(可直接复制)
import os
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd

def merge_excel():
folder_path = filedialog.askdirectory(title="请选择存放多个Excel的文件夹")
if not folder_path:
return

all_data = []
for file in os.listdir(folder_path):
if file.endswith(".xlsx"):
file_full = os.path.join(folder_path, file)
df = pd.read_excel(file_full)
all_data.append(df)

if not all_data:
messagebox.showwarning("提示", "该文件夹下没有找到Excel文件")
return

result = pd.concat(all_data, ignore_index=True)
save_path = os.path.join(folder_path, "合并结果.xlsx")
result.to_excel(save_path, index=False)

messagebox.showinfo("完成", f"合并成功!\n已保存到:\n{save_path}")

# ========== 创建窗口 ==========
root = tk.Tk()
root.title("Excel多表合并工具")
root.geometry("450x200")

tk.Label(root, text="Excel 批量合并工具", font=("微软雅黑", 14)).pack(pady=20)
tk.Button(root, text="选择文件夹并开始合并", font=("", 12), command=merge_excel, width=25, height=2).pack()

root.mainloop()
运行后,会直接弹出一个图形窗口,点按钮就能用。

四、代码超简单说明
tkinter:Python 内置可视化库,做窗口不用额外安装
filedialog.askdirectory:弹出选择文件夹窗口
messagebox:弹出成功/失败提示
核心逻辑还是前面的合并代码,只是套了一层界面
你甚至可以把它改成:
批量重命名工具
Excel 自动统计工具
自动发邮件工具

五、进阶:打包成 exe,发给任何人都能用
安装打包工具:
pip install pyinstaller
在终端运行打包命令:
pyinstaller -F -w 工具名.py
就能生成一个 .exe 文件,双击打开即用,不用装 Python。

下期不见不散~

福建,16分钟前,

最新文章

随机文章