Home > Net >  Can't export multiple DataFrames to different sheets in Excel using XlsxWriter
Can't export multiple DataFrames to different sheets in Excel using XlsxWriter

Time:05-09

I am using XlsxWriter, and trying to export 2 DFs to 2 different Sheets in Excel. I followed this enter image description here

But, if I would have used engine as openpyxl instead of xlsxwriter, I can see both the dfs exported into Sheet1 & Sheet2.

I think, there is a small tweak, but can't figure it out.

CodePudding user response:

Remove writer.save(). Calling to_excel has already saved the data.

The to_excel docs explain you only need to use a different sheet name to save to different sheets.

Multiple sheets may be written to by specifying unique sheet_name.

This is shown in the doc examples too:

df2 = df1.copy()
with pd.ExcelWriter('output.xlsx') as writer:  
    df1.to_excel(writer, sheet_name='Sheet_name_1')
    df2.to_excel(writer, sheet_name='Sheet_name_2')
  • Related