how to open pdf file using pypdf2

Question:

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?

Asked By: user13720131

||

Answers:

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)
Answered By: Peter Wood

A simple program to open a pdf file and print its first page will be as following,

import PyPDF2 

pdfFileObj = open('example.pdf', 'rb') 

pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 

print(pdfReader.numPages) # printing number of pages in pdf


pageObj = pdfReader.getPage(0) 


print(pageObj.extractText()) # extracting text from page 0


pdfFileObj.close() 
Answered By: Prathamesh

To open a pdf file you can write this code

import os
os.startfile('sample.pdf')

I hope this works!

Answered By: Shashwat Pritish
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.