Search code examples
excelcorrupt

How to fix a workbook that has been corrupted by openpyxl?


I am working with openpyxl to make changes to a file and then save it.

Among the lines that are used to this, the one that gives me trouble is:

wb = openpyxl.load_workbook(filename=sheet_loc, read_only=False,keep_vba=True)

I know for a fact that the "keep_vba" is a very dangerous parameter, because if you call it True when you are saving a .xlsx file, the file will get corrupted. And vice versa.

Now, this is exatcly what happened to me. I had a .xlsx file and I wrote "keep_vba=True", so the workbook is corrupted. This is what I receive when I'm trying to open it:

My question is: Is there any way to fix that ? To recover this sheet ? I've tried "Open and repair" button, but it didn't work, it just gives me the same error said above.


Solution

  • I was having this same problem and I got the solution today.

    To clarify, what you did here with openpyxl was nothing more than "turning" vba on for this workbook, but since the workbook still is a .xlsx file, it does not support this kind of thing.

    To recover this file, you just have to change the type of the file to .xlsm, you can do this on windows by simply renaming it from "workbook.xlsx" to "workbook.xlsm".