I do not know how relevant this is for the person who asked the question, but I am dealing with the same problem and found a possible solution.
1) open the source file (say: 1.xlsm) and do the magic with openpyxl
2) save as 2.xlsx
3) both files are actually archived files: extract them to temporary directories
4) copy the files from the source file directory to the xlsx directory: one of the files is a macro (vbaProject.bin), and 2 files are necessary because they describe the file type among other things
5) put all the files belonging to the xlsx file back into the zip file, and rename it from zip to xlsm. This file contains the original macro and was edited using openpyxl
Optional: 6) delete two temporary directories and a 2.xlsx file
Code example:
import openpyxl import zipfile from shutil import copyfile from shutil import rmtree import os PAD = os.getcwd() wb = openpyxl.load_workbook('1.xlsm')
Joost source share