pandas to excel add another sheet in existing excel file 1

pandas to excel add another sheet in existing excel file

import pandas as pd
from openpyxl import load_workbook
 
FilePath = "your excel path"
ExcelWorkbook = load_workbook(FilePath)
writer = pd.ExcelWriter(FilePath, engine = 'openpyxl')
writer.book = ExcelWorkbook
df.to_excel(writer, sheet_name = 'your sheet name')
writer.save()
writer.close()

Here is what the above code is Doing:
1. Load the workbook
2. Create a writer object
3. Set the writer’s workbook to the workbook you loaded
4. Write the dataframe to the writer
5. Save the writer
6. Close the writer

Similar Posts