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
  • 842
  • 2
  • 16
  • 32
Ratha
  • 9,434
  • 17
  • 85
  • 163

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
  • 105,104
  • 32
  • 201
  • 196