使用pandas将多个具有多个Sheet的excel文件合并成一个excel文件
import pandas as pd
import os
path = r'C:\files' #指定文件夹路径
wj_List = list(os.walk(path))[0][2] #所有子文件名
xls_file = pd.ExcelFile(path+'\\'+wj_List[0])
sheet_names = xls_file.sheet_names #获取excel文件的所有sheet名
writer = pd.ExcelWriter(path+'\\'+'result.xlsx')#设置excel框架,保证多次写入sheet时不会去被覆盖
for sheet in sheet_names:
dfs = []
for fn in wj_List:
dfs.append(pd.read_excel(path+'\\'+fn, sheet))
pd.concat(dfs).to_excel(writer,sheet, index=False)
writer.save()
writer.close()
赞 (0)