Problem to save a xlsm with Python (openpyxl)

396 Views Asked by At

I trying to open an Excel with macros, modify a cell and save the excel with macros again, however is not working.

Here is my code:

from openpyxl import load_workbook
 
#load excel file
workbook = load_workbook(r'FFM102-L-10-EBCL9.xlsm', read_only=False, keep_vba=True)
 
#open workbook
sheet = workbook['Track']
 
#modify the desired cell
sheet["B3"] = 5000

 
#save the file
workbook.save(r"FFM102-L-10-EBCL9.xlsm")

this is the answer:

Traceback (most recent call last): File "write_existing.py", line 56, in workbook.save(r"FFM102-L-10-EBCL9.xlsm") File "C:\Users\z004eu7h\AppData\Roaming\Python\Python310\site-packages\openpyxl\workbook\workbook.py", line 386, in save save_workbook(self, filename) File "C:\Users\z004eu7h\AppData\Roaming\Python\Python310\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook writer.save() File "C:\Users\z004eu7h\AppData\Roaming\Python\Python310\site-packages\openpyxl\writer\excel.py", line 275, in save self.write_data() File "C:\Users\z004eu7h\AppData\Roaming\Python\Python310\site-packages\openpyxl\writer\excel.py", line 60, in write_data archive.writestr(ARC_APP, tostring(props.to_tree())) File "C:\Users\z004eu7h\AppData\Local\Programs\Thonny\lib\zipfile.py", line 1814, in writestr with self.open(zinfo, mode='w') as dest: File "C:\Users\z004eu7h\AppData\Local\Programs\Thonny\lib\zipfile.py", line 1178, in close self._fileobj.seek(self._zinfo.header_offset) OSError: [Errno 9] Bad file descriptor

0

There are 0 best solutions below