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?
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)
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()
To open a pdf file you can write this code
import os
os.startfile('sample.pdf')
I hope this works!
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?
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)
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()
To open a pdf file you can write this code
import os
os.startfile('sample.pdf')
I hope this works!