PyPDF2.py 合并pdf时报错问题
生活随笔
收集整理的這篇文章主要介紹了
PyPDF2.py 合并pdf时报错问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
報錯如下:
Traceback (most recent call last):
File "./pdf_merge.py", line 68, in <module>
main(sys.argv[1], sys.argv[2])
File "./pdf_merge.py", line 51, in main
blankpage.mergeTranslatedPage(pageobj, 0, 0)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2377, in mergeTranslatedPage
tx, ty], expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2328, in mergeTransformedPage
PageObject._addTransformationMatrix(page2Content, page2.pdf, ctm), ctm, expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2284, in _mergePage
page2Content, rename, self.pdf)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2189, in _contentStreamRename
op = operands[i]
KeyError: 0
解決:
打開 /Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py
在文件開頭加上 import types
找到函數 _contentStreamRename
修改函數:
def _contentStreamRename(stream, rename, pdf):
if not rename:
return stream
stream = ContentStream(stream, pdf)
for operands, operator in stream.operations:
# for i in range(len(operands)):
# op = operands[i]
# if isinstance(op, NameObject):
# operands[i] = rename.get(op,op)
if type(operands) == types.ListType:
for i in range(len(operands)):
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
elif type(operands) == types.DictType:
for i in operands:
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
else:
raise KeyError("type of operands is %s" % type(operands))
return stream
_contentStreamRename = staticmethod(_contentStreamRename)
參考鏈接:https://github.com/mstamy2/PyPDF2/issues/196
總結
以上是生活随笔為你收集整理的PyPDF2.py 合并pdf时报错问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: swift:高级运算符(位运算符、溢出运
- 下一篇: Light OJ 1007