Home > other >  excelwings - repair file automatically to read file
excelwings - repair file automatically to read file

Time:03-21

I am working on excelwings to read an excel file.

However, the excel file is not something that I create. It is shared file used by multiple departments and it has some inbuilt formatting etc.

So, I don't disturb the existing formatting. So, I just plainly write data to that excel file and a specific sheet.

After I write data to the excel file, I use xlwings to read the file.

However, this results in an error.

But when I manually open the excel file, I get the below message.

enter image description here

Once I click OK, I get the below message

enter image description here

Once I click Close,my file name has the keyword repaired and excelwings is able to read the file successfully (once file is repaired)

sales = xw.Book('format_test.xlsx') # works fine after file repaired

How can I suppress this error or avoid this issue and allow excelwings to read the file without me manually repairing the file?

If I don't repair the file and allow excelwings to read the file, I get the below error

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)

CodePudding user response:

Use the corruptload parameter in Book():

import xlwings as xw

path = r"test.xlsx"

with xw.App(visible=False) as app:
    app.display_alerts = False
    wb = xw.Book(path, corrupt_load=1)

    wb.close()

You have to use the integer 1 as argument of corrupt_load, see documentation and here.

  • Related