writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
Sorted_Dataframe_1.to_excel(writer, sheet_name='Sheet1')
Sorted_Dataframe_2.to_excel(writer, sheet_name='Sheet2')
writer.save()
writer.close()
I have 2 DataFrames which I want to save to an existing excel named 'Output' to Sheet1 and Sheet2, respectively (see code). I am working in Sheet3 in excel itself, and every time I run the code, it deletes the Sheet3, and thus overwrites the whole excel. Is there any function to just 'update' sheets 1 and 2 but not touch the other sheets that I do not recall by the function in the 'Output' xlsx?
Can you set the mode to 'a' (for append). See the Pandas ExcelWriter docs .
writer = pd.ExcelWriter('Output.xlsx', mode='a', engine='xlsxwriter')
Xlsxwriter can only write new excel files from scratch, it cannot append to existing ones. This is explained here :
It cannot read or modify existing Excel XLSX files.
openpyxl supports reading from and appending to existing files, so you should set
with pd.ExcelWriter('output.xlsx',
mode='a', engine ='openpyxl') as writer:
df.to_excel(writer, sheet_name='Sheet_name_3')
This comes from the docs , but I have added engine ='openpyxl'
without which I am not sure it would work.
A very similar question was here: Append a sheet to an existing excel file using openpyxl
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.