这是代码示例,
import PyPDF2
import numpy as np
# creating a pdf file object
pdfFileObj = open('original.pdf' , 'rb')
pdfFileObj_1 = open('tutorial.pdf', 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdfReader_1 = PyPDF2.PdfFileReader(pdfFileObj_1)
# creating a pdf writer object for new pdf
pdfWriter = PyPDF2.PdfFileWriter()
for i in range(100):
page= pdfReader.getPage(i)
page_1= pdfReader_1.getPage(i)
pdfWriter.addPage(page)
pdfWriter.addPage(page_1)
#print(pdfWriter.getNumPages())
# new pdf file object
newFile = open('replaced_pdf_1.pdf', 'wb')
pdfWriter.write(newFile)
# closing the original pdf file object
pdfFileObj.close()
pdfFileObj_1.close()
# closing the new pdf file object
newFile.close()
我得到的错误
PdfReadWarning:未定义对象321 0。 [pdf.py:1629]追溯 (最近一次通话最近一次):文件“ test.py”,位于第22行 pdfWriter.write(newFile)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, 第482行,写入 self._sweepIndirectReferences(externalReferenceMap,self._root)文件 “ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第571行 self._sweepIndirectReferences(externMap,realdata)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第547行 值= self._sweepIndirectReferences(externMap,值)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第571行 self._sweepIndirectReferences(externMap,realdata)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第547行 值= self._sweepIndirectReferences(externMap,值)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第556行 值= self._sweepIndirectReferences(externMap,data [i])文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第571行 self._sweepIndirectReferences(externMap,realdata)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第547行 值= self._sweepIndirectReferences(externMap,值)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, _sweepIndirectReferences中的第577行 newobj = data.pdf.getObject(data)文件“ /home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”, 第1631行,在getObject中 引发utils.PdfReadError(“找不到对象。”)PyPDF2.utils.PdfReadError:找不到对象。
通过更改添加到PdfFileWriter
对象pdfWriter
的页面数,我了解到..如果页面超过5个左右,则显示上述错误..其他工作正常。我需要翻页超过100 ..请任何人对此提供帮助。
答案 0 :(得分:1)
I used this sample code on Windows 10 and Red Hat Enterprise Linux 6. On both platforms I used python 2.7 (I don't have python 3.5 on my workstation). As you didn't provide your versions of original.pdf and tutorial.pdf, I used 2 e-books in pdf format: 686 pages and 1014 pages respectively.
And I could not confirm your observations: with
for i in range(100):
replaced with
for i in range(600):
I received 1200-paged output pdf.