0

(1) I am trying to write to an existing XLSX file without erasing two tabs that already exist in the file. When I run the code chunk below, into my existing file, it overwrites sheets not named in the code. Can anyone suggest how I can write to my file without over-writing my work?

(2) Related, the suggested code for importing DateTime into XLSX as a date format didn't work for me, so I had to manually change the XLSX columns to date format. Is there a better way of confirming a datetime64 column will convert correctly to Date in XLSX?

path = r"/file/filename.xlsx" 

writer = pd.ExcelWriter(path, engine = 'xlsxwriter',date_format='mm dd yyyy')

df1.to_excel(writer, sheet_name = 'sheet1')

df2.to_excel(writer, sheet_name = 'sheet2')

df3.to_excel(writer, sheet_name = 'sheet3')

df4.to_excel(writer, sheet_name = 'sheet4')

writer.save()
j__carlson
  • 1,346
  • 3
  • 12
  • 20
PG_CB
  • 1

1 Answers1

0

Using append mode should fix it.

writer = pd.ExcelWriter(path, engine="xlsxwriter", date_format="mm dd yyyy", mode="a")
ogdenkev
  • 2,264
  • 1
  • 10
  • 19
  • @PG_CB, did this solve your issue? Is so would you please accept the answer so others know it worked? Thanks! – ogdenkev Oct 04 '21 at 02:17