is there any way to create a .xlsm file from scratch in python?
Thanks to both Alexander Pushkarev and APhillips for helping out with this question. Going off of Alexander's post I was able to figure out a hack to get this to work. I'm not really proud of this, but it works.
Running Alexander's code I get this error:
Exception ignored in:
Traceback (most recent call last): File
"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py",
line 1819, in del
self.close() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py",
line 1836, in close
self.fp.seek(self.start_dir) ValueError: I/O operation on closed file.
I played around with this and found out that if I took out keep_vba=True
from the load_workbook
function the code ran but I still got the error I noted above when trying to open the .xlsm
file with Excel.
So, looking at the latest error I saw the last line says
I/O operation on closed file.
I looked at openpxyl documentation and tried opening the file without the keep_vba=True
option before opening it with keep_vba=True
and it worked.
So excuse this ugly code, but this will work to create a .xlsm
file from scratch without depending on any existing files (copy and paste ready):
from openpyxl import Workbook
from openpyxl import load_workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
wb.save('new_document.xlsm')
wb1 = load_workbook('new_document.xlsm')
wb2 = load_workbook('new_document.xlsm', keep_vba=True)
wb2.save('new_document.xlsm')
Writing to existing xlsm files
No, it means that if there is a file with the same name, it will not warn you, it will just replace the file.
You can modify existing excel files, however, it doesn't save modifications, rather it overwrites the old file with a new file with modifications.
Related Topics
Check If Values of Multiple Columns Are the Same (Python)
How to Create Multiple Data Frames Using a for Loop in Python
Replacing Pandas or Numpy Nan With a None to Use With Mysqldb
Changing Presence Discord Status
How to Simply Add a Column Level to a Pandas Dataframe
How to Display the Value of the Bar on Each Bar With Pyplot.Barh()
High Pass Filter for Image Processing in Python by Using Scipy/Numpy
How to Determine Whether a Column/Variable Is Numeric or Not in Pandas/Numpy
Python: How to Match Nested Parentheses With Regex
How to Locate Elements on Webpage With Headless Chrome
Reading Columns of a Txt File on Python
Beautifulsoup Findall() Given Multiple Classes
Python Login Script; Usernames and Passwords in a Separate File
Python Tkinter How to Update a Text Widget in a for Loop
Webdriverexception: Message: Service Chromedriver Unexpectedly Exited. Status Code Was: 127
Getting the Id of the Last Record Inserted for Postgresql Serial Key With Python