我用python编写了一个脚本,使用pywin32将pdf文件保存为文本,直到最近为止,该脚本运行良好。我在Excel中使用类似的方法。代码如下:
def __pdf2Txt(self, pdf, fileformat="com.adobe.acrobat.accesstext"):
outputLoc = os.path.dirname(pdf)
outputLoc = os.path.join(outputLoc, os.path.splitext(os.path.basename(pdf))[0] + '.txt')
try:
win32com.client.gencache.EnsureModule('{E64169B3-3592-47d2-816E-602C5C13F328}', 0, 1, 1)
adobe = win32com.client.DispatchEx('AcroExch.App')
pdDoc = win32com.client.DispatchEx('AcroExch.PDDoc')
pdDoc.Open(pdf)
jObject = pdDoc.GetJSObject()
jObject.SaveAs(outputLoc, "com.adobe.acrobat.accesstext")
except:
traceback.print_exc()
return False
finally:
del jObject
pdDoc.Close()
del pdDoc
adobe.Exit()
del adobe
然而,此代码突然停止工作,我得到以下输出:
^{pr2}$我有类似的代码是用VB编写的,可以正常工作,所以我猜这与COM接口没有正确绑定到适当的函数有关?(我的COM知识不多)。在
Blish,this thread包含您要查找的解决方案的密钥:https://mail.python.org/pipermail/python-win32/2002-March/000260.html
我承认上面的帖子不是最容易找到的(可能是因为谷歌根据内容的年龄得分很低?)。在
具体地说,应用建议的this piece将为您运行:https://mail.python.org/pipermail/python-win32/2002-March/000265.html
作为参考,完整的一段代码,不需要您手动修补动态.py(代码片段应该是开箱即用的):
我已经在winpythonx64 2.7.6.3,acrobatxpro上测试过
在makepy.py是win32com python包附带的脚本。在
运行它进行安装,将python“连接”到Windows中的COM/OLE对象中。下面是一些代码的节选,我曾经与Excel交谈并在其中做一些事情。此示例获取当前工作簿中工作表1的名称。如果出现异常,它将自动运行makepy:
相关问题 更多 >
编程相关推荐