Opening corrupted Excel file with Python via COM interface

1.5k Views Asked by At

I'm trying to open an excel file using COM interface in Python. Normally it's easy but this time I have problem with opening the file that is corrupted. Error I get looks like this (partially in Polish):

com_error: (-2147352567, 'Wyst\xb9pi\xb3 wyj\xb9tek.', (0, u'Microsoft Excel', u'Open method of Workbooks class failed', u'xlmain11.chm', 0, -2146827284), None)

I coped with such problem previously in VBA by using additional parameter corruptload:=xlRepairFile in Open method. Do you have any idea how to do it in Python?

Below code doesn't work.

excel.Workbooks.Open(latest_file, CorruptLoad = "xlRepairFile")
1

There are 1 best solutions below

1
Alex Taylor On

Try:

excel.Workbooks.Open(latest_file, CorruptLoad=1)

There's an example here of someone getting it working. Their full example is:

xlApp = Dispatch("Excel.Application")
wb1=xlApp.Workbooks.Open(inputfile,ReadOnly=1,CorruptLoad=1)
xlApp.SendKeys("{Enter}",Wait=1)
xlApp.DisplayAlerts = 0
xlApp.Quit()
del xlApp

They also note:

The DisplayAlerts is needed to prevent Excel from asking if it should save a file that was opened as ReadOnly in the first place.