import pandas as pd import glob import os def merge_excel_files(source_dir="remotecontrol", output_file="merged_all_files.csv"): """ 将指定目录下的所有 Excel 文件 (.xlsx, .xls) 合并为一个 CSV 文件。 """ print(f"🔍 正在扫描目录: {source_dir} ...") # 支持 xlsx 和 xls files_xlsx = glob.glob(os.path.join(source_dir, "*.xlsx")) files_xls = glob.glob(os.path.join(source_dir, "*.xls")) files = files_xlsx + files_xls if not files: print("⚠️ 未找到 Excel 文件。") return # 按文件名中的数字进行排序 (例如: 1.xlsx, 2.xlsx, ..., 10.xlsx) try: files.sort(key=lambda x: int(os.path.basename(x).split('.')[0])) print("🔢 已按文件名数字顺序排序") except ValueError: # 如果文件名不是纯数字,退回到字母排序 files.sort() print("🔤 已按文件名包含非数字字符,使用字母顺序排序") print(f"📂 找到 {len(files)} 个文件: {files}") all_dfs = [] for file in files: try: print(f"📖 读取: {file}") # 使用 ExcelFile 读取所有 sheet xls = pd.ExcelFile(file) print(f" 📑 包含 Sheets: {xls.sheet_names}") file_dfs = [] for sheet_name in xls.sheet_names: df = pd.read_excel(xls, sheet_name=sheet_name) if not df.empty: print(f" ✅ Sheet '{sheet_name}' 读取成功: {len(df)} 行") file_dfs.append(df) else: print(f" ⚠️ Sheet '{sheet_name}' 为空,跳过") if file_dfs: # 合并该文件的所有非空 sheet file_merged_df = pd.concat(file_dfs, ignore_index=True) # 可选:添加一列标记来源文件 file_merged_df['Source_File'] = os.path.basename(file) all_dfs.append(file_merged_df) else: print(f"⚠️ 文件 {file} 所有 Sheet 均为空") except Exception as e: print(f"❌ 读取 {file} 失败: {e}") if all_dfs: print("🔄 正在合并数据...") merged_df = pd.concat(all_dfs, ignore_index=True) # 按 SendTime 排序 if 'SendTime' in merged_df.columns: print("⏳ 正在按 SendTime 排序...") merged_df['SendTime'] = pd.to_datetime(merged_df['SendTime'], errors='coerce') merged_df = merged_df.sort_values(by='SendTime') else: print("⚠️ 未找到 SendTime 列,跳过排序") print(f"💾 保存到: {output_file}") merged_df.to_csv(output_file, index=False, encoding="utf-8-sig") print(f"✅ 合并及排序完成!总行数: {len(merged_df)}") print(f" 输出文件: {os.path.abspath(output_file)}") else: print("⚠️ 没有成功读取到任何数据。") if __name__ == "__main__": # 如果需要在当前目录运行并合并 remotecontrol 文件夹下的内容 merge_excel_files(source_dir="remotecontrol", output_file="remotecontrol_merged.csv")