一、先说说我们遇到的真实场景项目需求很明确全国 30 省份每个省份下辖市 / 区独立 Excel 文件格式统一为.xlsx每个文件 1 个主数据表第一行为表头爬取完成后必须精准统计每个 Excel 文件的有效数据行数剔除表头所有文件总行数汇总自动识别空文件、无法读取的异常文件输出可存档的统计报表方便对账、验收、汇报手动操作的坑我们直接帮你踩过100 个 Excel 逐个打开看行数至少半小时起步还容易数错文件一多根本记不住哪个文件多少行后续核对完全没依据。高效的解法只有一个用自动化工具批量处理10 秒出结果全程不用手动点开任何文件。二、这款统计工具能解决什么问题我们打磨的这款Excel 批量行数统计工具完全适配爬虫归档、多文件数据盘点场景核心能力拉满✅ 全目录扫描指定文件夹自动识别所有.xlsx/.xls文件✅ 精准计数支持单 / 多 Sheet可自定义是否剔除表头✅ 异常兼容跳过加密文件、损坏文件、空表不中断运行✅ 可视化输出控制台实时打印进度同步生成 Excel 统计报表✅ 低门槛使用Python 环境 两行安装命令零基础也能跑✅ 业务适配完美匹配分省、分市、分地区拆分存储的爬虫数据三、完整工具代码复制即可运行环境准备仅 2 步先安装依赖库打开命令提示符执行pip install pandas openpyxl完整统计工具代码新建.py文件复制以下代码只需要修改文件夹路径就能直接运行123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120import osimport pandas as pdimport tkinter as tkfrom tkinter import ttk, filedialog, messageboximport threadingclass ExcelCounterApp:definit(self, root):self.root rootself.root.title(“ 目录Excel内容条数统计工具”)self.root.geometry(“750x550”)self.root.resizable(False, False)# 变量 self.target_path tk.StringVar() self.total_count 0 # 顶部路径选择 tk.Label(root, text目标文件夹).place(x20, y20) tk.Entry(root, textvariableself.target_path, width60).place(x100, y20) tk.Button(root, text选择文件夹, commandself.select_path, bg#4CAF50, fgwhite).place(x520, y15) # 按钮 self.start_btn tk.Button(root, text开始统计, commandself.start_count, bg#2196F3, fgwhite, width15) self.start_btn.place(x20, y60) tk.Button(root, text清空结果, commandself.clear_result, bg#f44336, fgwhite, width15).place(x180, y60) # 状态 self.status_label tk.Label(root, text状态等待操作, fgblue) self.status_label.place(x350, y65) # 表格展示 self.tree ttk.Treeview(root, columns(目录, 文件数, 总行数), showheadings) self.tree.heading(目录, text目录路径) self.tree.heading(文件数, textExcel文件数量) self.tree.heading(总行数, text内容总条数) self.tree.column(目录, width450) self.tree.column(文件数, width100) self.tree.column(总行数, width120) self.tree.place(x20, y100, width700, height400) # 汇总 self.summary_label tk.Label(root, text 汇总共 0 个目录 | 总计 0 条数据, font(Arial, 12, bold)) self.summary_label.place(x20, y510) def select_path(self): path filedialog.askdirectory() if path: self.target_path.set(path) def clear_result(self): for item in self.tree.get_children(): self.tree.delete(item) self.total_count 0 self.status_label.config(text状态已清空) self.summary_label.config(text 汇总共 0 个目录 | 总计 0 条数据) def start_count(self): path self.target_path.get().strip() if not os.path.isdir(path): messagebox.showerror(错误, 请选择有效文件夹) return self.start_btn.config(statetk.DISABLED) self.status_label.config(text状态正在统计...) self.clear_result() # 子线程运行防止界面卡死 thread threading.Thread(targetself.do_count, args(path,)) thread.daemon True thread.start() def do_count(self, root_path): dir_data {} # key:目录路径 value:[文件数, 总行数] excel_ext (.xlsx, .xls, .csv) try: # 遍历所有目录和文件 for folder_path, _, files in os.walk(root_path): file_count 0 row_count 0 for file in files: if file.lower().endswith(excel_ext): file_path os.path.join(folder_path, file) try: if file.endswith(.csv): df pd.read_csv(file_path, on_bad_linesskip) else: df pd.read_excel(file_path, sheet_nameNone) row_count sum(len(sheet) for sheet in df.values()) file_count 1 except Exception as e: print(f读取失败{file_path} {str(e)}) if file_count 0: dir_data[folder_path] [file_count, row_count] # 统计完成更新界面 total_dir len(dir_data) total_rows sum(v[1] for v in dir_data.values()) for folder, (f_cnt, r_cnt) in dir_data.items(): self.tree.insert(, tk.END, values(folder, f_cnt, r_cnt)) self.status_label.config(text状态✅ 统计完成, fggreen) self.summary_label.config( textf 汇总共 {total_dir} 个目录 | 总计 {total_rows} 条数据, fgred ) except Exception as e: messagebox.showerror(异常, f统计出错{str(e)}) self.status_label.config(text状态❌ 统计失败, fgred) finally: self.start_btn.config(statetk.NORMAL)ifname “main”:window tk.Tk()app ExcelCounterApp(window)window.mainloop()四、工具输出结果说明运行后会得到两份核心结果image五、适配爬虫场景的实用技巧针对全国分省爬虫数据归档这类高频场景补充 3 个实用优化点表头适配调整若你的 Excel 无表头把代码中skip_headerTrue改为False直接统计全行数。