The following is a simple snippet to open a .xlsm file, write a few values to it with python, and save it.
import openpyxl
from openpyxl import load_workbook
def toExcel():
wb = load_workbook(filename="C:\\Users\\Mark\\Documents\\Test.xlsm")
ws = wb.worksheets[0]
ws.cell(row=1, column=1).value = 'foo'
ws['A2'] = 'bar'
wb.save("C:\\Users\\Mark\\Documents\\Test1.xlsm")
toExcel()
While the file opens and saves, it mentions file format not valid / corrupt and cannot open. If the .xlsm is removed from the wb.save, it will save and open after selecting excel with Open With. Why is the file format not valid as is?
.xlsx
openpyxl.readthedocs.io/en/default/… read the last note here.xlsx
with apparenetly some extras, it wouldn't open if sent in a response as "xlsx". It did open in libre office. When the file.extension was changed toxlsm
the file could be opened.