'Write to an existing .xlsm using pandas and XlsxWriter

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 *



Solution 1:[1]

I had the same issue using this XlsxWriter workaround. I think you would need to copy/paste already existing sheets into writer.sheets. Maybe one way to fix it is to add a line like

writer.sheets = {ws.title: ws for ws in writer.book.worksheets}

but this syntax does not work with engine = 'xlsxwriter'

To add a DataFrame to an xlsm sheet what I did is simply :

import pandas
import openpyxl

writer = pandas.ExcelWriter(excel_file_path, engine='openpyxl')

writer.book = openpyxl.load_workbook(excel_file_path, keep_vba= True)
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}

dataframe_to_insert.to_excel(writer, sheet_name= sheet_destination)

workbook = writer.book
workbook.filename = excel_file_path

writer.save()
writer.close()

It seems keep_vba= True is doing the job for me (Python 3.8.x).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1