简单给别人写的脚本记录一下。
import os
import pandas as pd
source_path = 'D:\\projects\\excel_merge\\test_source'
dist_file_path = '.\\text.xlsx'
# 递归获取目录(文件夹)下的所有文件路径
def get_file_path(dir_path):
res_file_path_list = []
def get_filepath_recur(dir_path, list_name):
"""递归获取目录下(文件夹下)所有文件的路径"""
for file in os.listdir(dir_path): # 获取文件(夹)名
file_path = os.path.join(dir_path, file) # 将文件(夹)名补全为路径
if os.path.isdir(file_path): # 如果是文件夹,则递归
get_filepath_recur(file_path, list_name)
else:
list_name.append(file_path) # 保存路径
return list_name
res = get_filepath_recur(dir_path, res_file_path_list)
return res
# for i in get_file_path(source_path):
# print(i)
# path:所有需要合并的excel文件所在的文件夹
def read_merge_write(file_path_list, dist_file_path):
print(file_path_list)
df_list = []
for file_path in file_path_list:
df = pd.read_excel(file_path, engine='openpyxl')
df_list.append(df)
df_merged = pd.concat(df_list, axis=0)
# 预防数据精度缺失问题,统一都转为字符串格式
for col_name, series in df_merged.iteritems():
df_merged[col_name] = df_merged[col_name].astype(str)
writer = pd.ExcelWriter(dist_file_path)
df_merged.to_excel(writer, sheet_name='res', startcol=0, index=False)
writer.save()
return 0
read_merge_write(get_file_path(source_path), dist_file_path)