0

I’m writing dataframe to 5 Excel sheets using df.to_excel(). eg:

  df.to_excel(writer, sheet_name='Invoice details', index=False) 
  df.to_excel(writer, sheet_name='Invoice Summary', index=False) 

Is there a way to indicate how to arrange the sheets by order while writing them? That‘s, I want Invoice summary sheet should be first sheet while, invoice details sheet should be last sheet

abdoulsn
  • 708
  • 1
  • 11
  • 28
Ratha
  • 8,920
  • 16
  • 71
  • 141

1 Answers1

3

Use a with context manager, and then specify the files in the desired order.

df1 = pd.DataFrame({'total_invoices': [2]})
df2 = pd.DataFrame({'invoice_no': [1, 2]})

with pd.ExcelWriter('invoices.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Invoice Summary', index=False)
    df2.to_excel(writer, sheet_name='Invoice details', index=False)
Alexander
  • 96,739
  • 27
  • 183
  • 184