用另一个PDF替换PDF中至少100页的页面

问题描述 投票:1回答:1

这是代码的一个例子,

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“,第571行,_sweepIndirectReferences self._sweepIndirectReferences(externMap,realdata)文件”/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py“,第547行,在_sweepIndirectReferences值中= self._sweepIndirectReferences(externMap,value)文件“/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”,第571行,位于_sweepIndirectReferences中self._sweepIndirectReferences(externMap,realdata)文件“/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”,第547行,在_sweepIndirectReferences中= self._sweepIndirectReferences(externMap,value)文件“/ home / ubuntu /仅限Ritesh /工作/ lib中/ python3.5 /站点packag es / PyPDF2 / pdf.py“,第556行,在_sweepIndirectReferences中= self._sweepIndirectReferences(externMap,data [i])文件”/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/ pdf.py“,第571行,_sweepIndirectReferences self._sweepIndirectReferences(externMap,realdata)文件”/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py“,第547行, _sweepIndirectReferences value = self._sweepIndirectReferences(externMap,value)文件“/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”,第577行,位于_sweepIndirectReferences中newobj = data.pdf。 getObject(data)文件“/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py”,第1631行,getObject raise utils.PdfReadError(“找不到对象。”) PyPDF2.utils.PdfReadError:找不到对象。

我通过改变添加到PdfFileWriter对象的页面数量来理解pdfWriter ..如果页面大于5,它显示上面的错误..它的工作正常。我需要重新填写超过100页的页面..请任何人帮忙解决这个问题。

python pdf
1个回答
1
投票

我在Windows 10和Red Hat Enterprise Linux 6上使用了这个示例代码。在这两个平台上我使用了python 2.7(我的工作站上没有python 3.5)。由于您没有提供original.pdf和tutorial.pdf的版本,我使用了2种pdf格式的电子书:分别为686页和1014页。

我无法确认你的观察结果:

对于范围内的我(100):

换成了

对于范围内的我(600):

我收到了1200页的输出pdf。

© www.soinside.com 2019 - 2024. All rights reserved.