Mar-15-2018, 12:47 PM
Hi All,
My first post :)
I am working on an project at work where I have an xlsm file that I am updating cell values from multiple other xlsx files.
This all works as expected except but when I save as a .xlsm file and open the file in Excel I am missing buttons that are on sheet1.
I load the workbook only one time and I set keep_vba=True in load_workbook
I save the wb multiple times as I am updating in different functions in my code but I simple call wb.save at the end of each function where I changed some values.
Is the expected? That I will not get a valid xlsm file when saving off from openpyxl?
I have done some reading on this and realize that .xlsx and .xlsm are actually zip files so I tried some code from StackOverflow
I tried this code below but I get errors when opening the file in Excel. I also just tried pasting the two sheets that I modified into the unzipped original xlsm worksheet folders and still had problems loading. It loads but doesn't recalculate the formulas unless you go hit enter in a cell with the formula so that is not an option. I am trying to not re-write the updates in win32com basically
My first post :)
I am working on an project at work where I have an xlsm file that I am updating cell values from multiple other xlsx files.
This all works as expected except but when I save as a .xlsm file and open the file in Excel I am missing buttons that are on sheet1.
I load the workbook only one time and I set keep_vba=True in load_workbook
wb = load_workbook( filename = input_file, keep_vba=True )
I save the wb multiple times as I am updating in different functions in my code but I simple call wb.save at the end of each function where I changed some values.
wb.save(output_file_name)
Is the expected? That I will not get a valid xlsm file when saving off from openpyxl?
I have done some reading on this and realize that .xlsx and .xlsm are actually zip files so I tried some code from StackOverflow
I tried this code below but I get errors when opening the file in Excel. I also just tried pasting the two sheets that I modified into the unzipped original xlsm worksheet folders and still had problems loading. It loads but doesn't recalculate the formulas unless you go hit enter in a cell with the formula so that is not an option. I am trying to not re-write the updates in win32com basically
import openpyxl import zipfile from shutil import copyfile from shutil import rmtree import os PAD = os.getcwd() wb = openpyxl.load_workbook('1.xlsm') ##### # do magic with openpyxl here and save ws = wb.worksheets[0] ws.cell(row=2, column=3).value = 'Edited' # example ##### wb.save('2.xlsx') with zipfile.ZipFile('1.xlsm', 'r') as z: z.extractall('./xlsm/') with zipfile.ZipFile('2.xlsx', 'r') as z: z.extractall('./xlsx/') copyfile('./xlsm/[Content_Types].xml','./xlsx/[Content_Types].xml') copyfile('./xlsm/xl/_rels/workbook.xml.rels','./xlsx/xl/_rels/workbook.xml.rels') copyfile('./xlsm/xl/vbaProject.bin','./xlsx/xl/vbaProject.bin') z = zipfile.ZipFile('2.zip', 'w') os.chdir('./xlsx') for root, dirs, files in os.walk('./'): for file in files: z.write(os.path.join(root, file)) z.close()