熊猫ExcelWriter不会追加

时间:2019-09-08 05:41:47

标签: pandas

我有一个xlsx模板,该模板在第一页上包含一个仪表板,该仪表板从几张纸中读取数据。我的目标是写这些表而不替换其他表/仪表板的内容。我的代码如下:

import os
import pandas as pd
import openpyxl

dataframe1 = pd.read_excel('df1.xlsx')
dataframe2 = pd.read_excel('df2.xlsx')

with pd.ExcelWriter('template.xlsx', mode='a', engine = 'openpyxl') as 
writer:
dataframe1.to_excel(writer, sheet_name='sheet10')
dataframe2.to_excel(writer, sheet_name='sheet11')
writer.save()
writer.close()

不幸的是,这导致我的模板仅被这两张纸代替,而不是被附加到文件中。

1 个答案:

答案 0 :(得分:0)

import os
import glob
import pandas as pd
import openpyxl

all_data = pd.DataFrame()

#Here you'll extract all .xlsx file and append it in all_data

for f in glob.glob("*.xlsx"):
    df = pd.read_excel(f)
    all_data = all_data.append(df,ignore_index=True)

 #Now write data into output file

writeData = pd.ExcelWriter('template.xlsx')
all_data.to_excel(writeData,'sheet1')
writer.save()

writer.close()

也许这会对您有所帮助。