10

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?

4
  • 2
    What happens if you try making is .xlsx openpyxl.readthedocs.io/en/default/… read the last note here. Commented Jul 3, 2016 at 20:06
  • Yes just reading this. Cheers
    – Iorek
    Commented Jul 3, 2016 at 20:10
  • 1
    Possible duplicate of How to save XLSM file with Macro, using openpyxl Commented Jul 3, 2016 at 20:22
  • I can corroborate that mucking about with file.extension can solve this. I load an 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 to xlsm the file could be opened.
    – semiomant
    Commented Mar 4, 2019 at 10:48

3 Answers 3

6

I found this post because I was trying to create a .xlsm file from scratch using openpyxl. I figured out that I was getting this error because when you load the workbook you need to have keep_vba=True as the second parameter passed to the load_workbook function.

So this is what your load_workbook function should look like:

wb = load_workbook(filename="C:\\Users\\Mark\\Documents\\Test.xlsm", keep_vba=True)

As a side note, here is my post that talks about creating a .xlsm file from scratch using openpyxl.

1
  • 1
    That did it for me. Thank you Commented Jan 11, 2022 at 18:32
4

From here: https://openpyxl.readthedocs.io/en/default/tutorial.html#saving-to-a-file

Note

The following will fail:

>>> wb = load_workbook('document.xlsx')
>>> # Need to save with the extension *.xlsx
>>> wb.save('new_document.xlsm')
>>> # MS Excel can't open the document
>>>
>>> # or
>>>
>>> # Need specify attribute keep_vba=True
>>> wb = load_workbook('document.xlsm')
>>> wb.save('new_document.xlsm')
>>> # MS Excel can't open the document
>>>
>>> # or
>>>
>>> wb = load_workbook('document.xltm', keep_vba=True)
>>> # If us need template document, then we need specify extension as *.xltm.
>>> # If us need document, then we need specify attribute as_template=False.
>>> wb.save('new_document.xlsm', as_template=True)
>>> # MS Excel can't open the document
5
  • Yes I just read this, sadly. Are there additional libraries that will allow me to build in macros as well as python?
    – Iorek
    Commented Jul 3, 2016 at 20:10
  • Sorry, no idea. I typically only use basic excel functionality in my tasks. good luck! Commented Jul 3, 2016 at 20:12
  • 4
    wb = load_workbook(filename='C:\\Users\\Mark\\Documents\\Test.xlsm', read_only=False, keep_vba=True) This solved it, I can now save in xlsm after opening it like this
    – Iorek
    Commented Jul 3, 2016 at 20:29
  • nice, you should write it up as an answer under "answer my own question" Commented Jul 3, 2016 at 20:30
  • even with wb = load_workbook(filename='C:\\Users\\Mark\\Documents\\Test.xlsm', read_only=False, keep_vba=True) I cannot open excel and shows corrupted, im only writing in cells which have nothing to do with the macro buttons on the excel,anyone knows how to resolve?
    – Scope
    Commented Oct 18, 2020 at 20:58
0

You need to save to xlxm format first and then convert to xlxs:

wb = openpyxl.Workbook()

xlxm_tmp_file_path = "tmp_file.xlsm"
xlxs_local_file = "file.xlsx"
wb.save(xlxm_tmp_file)

wb = openpyxl.load_workbook(xlxm_tmp_file, keep_vba=True)
wb.save(xlxs_local_file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.