Search code examples
pythonpypdf

how to open pdf file using pypdf2


I tried to open a pdf file using pypdf in Google Colab using

import PyPDF2 as pdf2
with open("sample.pdf", "r+") as f:
   pdf = pdf2.PdfFileReader(f)

but I get following error:

UnsupportedOperation: can't do nonzero end-relative seeks

Changing the mode form "r" to "r+" does not resolve the problem. What is the cause of this error and how can I solve it?


Solution

  • According to this bug report, you need to open with mode='rb'.

    import PyPDF2 as pdf2
    
    with open ("sample.pdf", "rb") as f:
       pdf = pdf2.PdfFileReader(f)