gpt4 book ai didi

python - Python 中 Pypdf 包中的断言错误

转载 作者:行者123 更新时间:2023-11-28 18:53:21 25 4
gpt4 key购买 nike

我在 Windows 平台上使用 Python 2.4 和 PyPdf 1.13。我正在尝试使用以下代码将列表中的 PDF 文件合并为一个文件:

import os
from pyPdf import PdfFileWriter, PdfFileReader

attached=["C:\\test\\T_tech.pdf","C:\\test\\00647165-Backup.pdf"]
output=PdfFileWriter()
maxpage=0

os.chdir("C:\\test")
name= attached[0]
name = os.path.basename(name).split('.')[0]

for nam in attached:
input= PdfFileReader(file(nam,"rb"))
maxpage=input.getNumPages()
for i in range(0,maxpage):
output.addPage(input.getPage(i))

outputStream =file("Output-"+name+".pdf","wb")
output.write(outputStream)
outputStream.close()

运行此代码时出现以下错误。

  Traceback (most recent call last):
File "C:\Python24\pdfmerge.py", line 13, in ?
input= PdfFileReader(file(nam,"rb"))
File "C:\Python24\Lib\site-packages\pyPdf\pdf.py", line 374, in __init__
self.read(stream)
File "C:\Python24\Lib\site-packages\pyPdf\pdf.py", line 847, in read
assert False
AssertionError

非常感谢任何帮助。

最佳答案

来源:

            # bad xref character at startxref.  Let's see if we can find
# the xref table nearby, as we've observed this error with an
# off-by-one before.
stream.seek(-11, 1)
tmp = stream.read(20)
xref_loc = tmp.find("xref")
if xref_loc != -1:
startxref -= (10 - xref_loc)
continue
else:
# no xref table found at specified location
assert False
break

您遇到了后者“未找到交叉引用表...”的情况。尝试修补源代码,省略断言,看看它是否仍然有效。

关于python - Python 中 Pypdf 包中的断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8345476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com