python使用多个电子表格合并多个excel文件

时间:2016-10-16 03:39:54

标签: python excel

我想将表单合并不同的excel文件放在一起,所以我可以总结一下sheet1,sheet2等...但是现在我只能将表单分别放在一个工作簿中...任何帮助?谢谢你。

这是我的剧本:

import pandas as pd
import glob
import os
path_read =r'D:\merge'

list_dfs = []
data = pd.DataFrame()

for f in glob.glob(path_read + "/*.xls"):
    sheets = pd.ExcelFile(f).sheet_names    
    for i,sheet in enumerate(sheets):
        df = pd.read_excel(f, sheetname=sheets[i])        
        data = data.append(df)                
        list_dfs.append(data)
        data = pd.DataFrame()

xls_path = r'D:\mytable.xlsx'

def save_xls(list_dfs, xls_path):
    writer = pd.ExcelWriter(xls_path)
    for n, df in enumerate(list_dfs):
        df.to_excel(writer, 'sheet%s' % n)
    writer.save()

save_xls(list_dfs,xls_path)

0 个答案:

没有答案