Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 parse PDF from web

I was trying to get a PDF from a webpage, parse it and print the result to the screen using PyPDF2. I got it working without issues with the following code:

with open("foo.pdf", "wb") as f:
    f.write(requests.get(buildurl(jornal, date, page)).content)
pdfFileObj = open('foo.pdf', "rb")
pdf_reader = PyPDF2.PdfFileReader(pdfFileObj)
page_obj = pdf_reader.getPage(0)
print(page_obj.extractText())

Writing a file just so I can then read it though sounded wasteful, so I figured I'd just cut the middleman with this:

pdf_reader = PyPDF2.PdfFileReader(requests.get(buildurl(jornal, date, page)).content)
page_obj = pdf_reader.getPage(0)
print(page_obj.extractText())

This, however yields me an AttributeError: 'bytes' object has no attribute 'seek'. How can I feed the PDF coming from requests directly onto PyPDF2?

like image 916
Bernardo Meurer Avatar asked Dec 19 '22 14:12

Bernardo Meurer


2 Answers

You have to convert the returned content to a file-like object using BytesIO:

import io

pdf_content = io.BytesIO(requests.get(buildurl(jornal, date, page)).content)
pdf_reader = PyPDF2.PdfFileReader(pdf_content)
like image 62
DocZerø Avatar answered Dec 21 '22 05:12

DocZerø


Use io to fake the use of a file (Python 3):

import io

output = io.BytesIO()
output.write(requests.get(buildurl(jornal, date, page)).content)
output.seek(0)
pdf_reader = PyPDF2.PdfFileReader(output)

I did not test in your context but I tested this simple example and it worked:

import io

output = io.BytesIO()
output.write(bytes("hello world","ascii"))
output.seek(0)
print(output.read())

yields:

b'hello world'
like image 20
Jean-François Fabre Avatar answered Dec 21 '22 04:12

Jean-François Fabre