简体   繁体   中英

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

Using append mode should fix it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM