PG_CB
PG_CB

Reputation: 1

Writing to an XLSX without over-writing existing sheets

(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()

Upvotes: 0

Views: 51

Answers (1)

ogdenkev
ogdenkev

Reputation: 2374

Using append mode should fix it.

writer = pd.ExcelWriter(path, engine="xlsxwriter", date_format="mm dd yyyy", mode="a")

Upvotes: 0

Related Questions