def merge_excel_data(folder_path, result_file_path, file_name_list): """ :param folder_path: 文件夹路径 :param result_file_path: 汇总文件绝对路径 :param file_name_list: 文件名list :return: 汇总文件DataFrame """ # 默认sort排序,是按从小到大来的 file_name_list = sorted(file_name_list, key=lambda x: os.path.getmtime(os.path.join(folder_path, x))) target_list = [] for file in file_name_list: child = os.path.join(folder_path, file) target_list.append(child) # 读取汇总表 if os.path.exists(result_file_path): merge_df = pd.read_excel(result_file_path, keep_default_na=False, dtype=str) else: merge_df = DataFrame(dtype=str) today_df = DataFrame(dtype=str) for file_path in target_list: temporary_df = pd.read_excel(file_path, keep_default_na=False, dtype=str) today_df = today_df.append(temporary_df) merge_df = merge_df.append(today_df) merge_df.to_excel(result_file_path, index=False) return today_df