0

I used the following code to read data in file_1 then write that to a new file_2.

import pandas as pd
inventory = pd.read_excel('file_1.xlsx', skiprows=3)
inventory.to_excel('file_2.xlsx')

file_2 is a newly created file each time. How do I write the data to specific tab in an existing file without clearing out other tabs that contain data?

1 Answer 1

1

ExcelWriter can be used to append to an existing Excel file using mode='a'. Specify the sheet name with the sheet_name parameter.

with pd.ExcelWriter('file_2.xlsx', mode='a') as writer:  
    inventory.to_excel(writer, sheet_name='Sheet_name_1')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. The only part I don't understand is how it knows how to find file_2 with that code.
The file will be written to the default working directory unless otherwise specified.
This is helpful. Thank you. When I do that it keeps creating a new sheet. Is there a way to have it put the data in an existing sheet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.