Home>

I'm trying to write to a PDF file in Python.
When I run the program, an error is displayed (the error message will be described later), the size of the corresponding PDF file becomes 0KB, and it is corrupted.
Older PDF versions (1.2 (Acrobat 3.x), etc.) can be written without error, so I'm wondering if they may have been played due to different versions.
In addition, version 2020.012.20043 is used for Acrobat (Is it possible that the authority here may have an effect?).

I would like to borrow some wisdom about the solution.
I look forward to working with you.

import PyPDF2
src_pdf = PyPDF2.PdfFileReader ("Desktop \ test /test.pdf")
dst_pdf = PyPDF2.PdfFileWriter ()
dst_pdf.cloneReaderDocumentRoot (src_pdf)
with open ("Desktop \ test /test.pdf", "wb") as f:
    dst_pdf.write (f)


Error message
PdfReadError Traceback (most recent call last)
in
5 dst_pdf.cloneReaderDocumentRoot (src_pdf)
6 with open ("Desktop \ test /test.pdf", "wb") as f:
---->7 dst_pdf.write (f)

~ \ anaconda3 \ lib \ site-packages \ PyPDF2 \ pdf.py in write (self, stream)
480 self.stack = []
481 if debug: print (("ERM:", externalReferenceMap, "root:", self._root))
->482 self._sweepIndirectReferences (externalReferenceMap, self._root)
483 del self.stack
484

~ \ anaconda3 \ lib \ site-packages \ PyPDF2 \ pdf.py in _sweepIndirectReferences (self, externMap, data)
569 self.stack.append (data.idnum)
570 realdata = self.getObject (data)
->571 self._sweepIndirectReferences (externMap, realdata)
572 return data
573 else:

~ \ anaconda3 \ lib \ site-packages \ PyPDF2 \ pdf.py in _sweepIndirectReferences (self, externMap, data)
545 for key, value in list (data.items ()):
546 origvalue = value
->547 value = self._sweepIndirectReferences (externMap, value)
548 if is instance (value, StreamObject):
549 # a dictionary value is a stream. Streams must be indirect

~ \ anaconda3 \ lib \ site-packages \ PyPDF2 \ pdf.py in _sweepIndirectReferences (self, externMap, data)
575 if newobj == None:
576 try:
->577 newobj = data.pdf.getObject (data)
578 self._objects.append (None) #placeholder
579 idnum = len (self._objects)

~ \ anaconda3 \ lib \ site-packages \ PyPDF2 \ pdf.py in getObject (self, indirectReference)
1629 indirectReference.generation), utils.PdfReadWarning)
1630 #if self.strict:
->1631 raise utils.PdfReadError ("Could not find object.")
1632 self.cacheIndirectObject (indirectReference.generation,
1633 indirectReference.idnum, retval)

PdfReadError: Could not find object.