I would like to write a dataframe to an existing .xlsm file which already has content.
Write pandas dataframe to xlsm file (Excel with Macros enabled) describes how to write to a new file but I want to write to add a new sheet to an existing .xlsm file. When I use the following (as in the question):
import pandas as pd
df = pd.DataFrame({'First' : [5, 2, 0, 10, 4],
'Second' : [9, 8, 21, 3, 8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
workbook.filename = 'test.xlsm'
workbook.add_vba_project('./vbaProject.bin')
df.to_excel(writer, sheet_name='Sheet1') #This is the df I'm trying to add to the sheet
writer.save()
it overwrites the original content and the existing .xlsm file only has the newly added sheet.
I can't quite figure out how to amend the code in the linked question above to consider an existing file.
Thanks
*I have extracted the vbaProject.bin ad described here: https://xlsxwriter.readthedocs.io/working_with_macros.html *