0

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")
drafa
  • 81
  • 1
  • 7

1 Answers1

0

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.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • Unfortunately it doesn't help in my case. As a workaround I think about running VBA code from inside of Python but it doesn't seem to be the best practice... – drafa Nov 14 '17 at 17:55