Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel file corrupted after bulk saving with Python

The aim of my work is just to save multiple Excel Macro files with exactly the same name.
I used the openpyxl library for it. The only thing, which changes in my file is the number att the end.

My code looks as follows:

 from openpyxl import Workbook, load_workbook
 wb = load_workbook('Collections/Collection Note - 001.xlsm')
 ws = wb.active
 for x in range(2,25):
    newname = "%03d" % x
    wb.save('Collections/Collection Note - '+newname+'.xlsm')

Everything works well as the files come out. However, I have no faintest idea why are they corrupted?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is wrong here, since I want just open them and resave?

enter image description here

>Solution :

The problem is because you are using xlsm files (files with Macro-enable).

In this case youve to load the files differently:

load_workbook('Collections/Collection Note - 001.xlsm', read_only=False, keep_vba=True)

(Btw you could look inside your xlsm files by renaming them to filename.zip and check if there is stuff missing)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading