0

I am using this code to open the file to find the size and dimension of PDF.

from PyPDF2 import PdfFileReader
with open(SourceFilePath, 'rb') as f:
      input1 = PdfFileReader(f)

Most of the time I am able to open the PDF file using PdfFileReader from PyPDF2 works fine, but with few PDF it throws this error:

NumberObject object is not subscriptable

Can anyone please help me why it is happening and how can I solve it?

Kavita
  • 1
  • 2
  • 1
    When does the exception happen? Can you provide the complete stack trace, please? – Pythocrates Jul 03 '21 at 10:35
  • Hi Pythocrates, Thanks for the response. Below is the stack trace - '''input1 = PdfFileReader(f) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__ self.read(stream) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 1805, in read assert xrefstream["/Type"] == "/XRef" TypeError: 'NumberObject' object is not subscriptable''' – Kavita Jul 03 '21 at 11:01
  • Is it now a `NoneType object` or a `NumberObject`? – Pythocrates Jul 03 '21 at 11:05
  • It is NumberObject only, Sorry for the mistake. Updated the question. – Kavita Jul 03 '21 at 11:08
  • It sound like some broken/non-standard PDF. Maybe you can rewrite the PDF, for example using LibreOffice (or some PDF tool) as described here: https://stackoverflow.com/questions/49701323/merging-pdf-files-using-python-and-pypdf2-throws-a-typeerror – Pythocrates Jul 03 '21 at 11:12
  • Ok , thanks. Will try same. – Kavita Jul 03 '21 at 11:16

0 Answers0