Reputation: 13
This is an example of code,
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()
And the error i got,
PdfReadWarning: Object 321 0 not defined. [pdf.py:1629] Traceback (most recent call last): File "test.py", line 22, in pdfWriter.write(newFile) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 482, in write self._sweepIndirectReferences(externalReferenceMap, self._root) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 571, in _sweepIndirectReferences self._sweepIndirectReferences(externMap, realdata) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 547, in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap, value) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 571, in _sweepIndirectReferences self._sweepIndirectReferences(externMap, realdata) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 547, in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap, value) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 556, in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap, data[i]) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 571, in _sweepIndirectReferences self._sweepIndirectReferences(externMap, realdata) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 547, in _sweepIndirectReferences value = self._sweepIndirectReferences(externMap, value) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 577, in _sweepIndirectReferences newobj = data.pdf.getObject(data) File "/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py", line 1631, in getObject raise utils.PdfReadError("Could not find object.") PyPDF2.utils.PdfReadError: Could not find object.
What i understood from changing the number of pages added to the PdfFileWriter
object pdfWriter
..if the pages are more than around 5 it showing above error..else its working fine. I need to replce pages more than 100..please anyone help with this.
Upvotes: 1
Views: 652
Reputation: 104
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.
Upvotes: 1