0

我正在尝试使用 Python 中的 COM 接口打开一个 excel 文件。通常这很容易,但这次我在打开损坏的文件时遇到了问题。我得到的错误看起来像这样(部分是波兰语):

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)

我以前在 VBA 中通过corruptload:=xlRepairFile在 Open 方法中使用附加参数来解决此类问题。你知道如何在 Python 中做到这一点吗?

下面的代码不起作用。

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

1 回答 1

0

尝试:

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

这里有一个例子,有人让它工作。他们的完整示例是:

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

他们还指出:

需要 DisplayAlerts 以防止 Excel 询问它是否应该首先保存以只读方式打开的文件。

于 2017-11-13T21:34:52.417 回答