Background: Python 3.7 & pdfminer.six
Using the information found here: Exporting Data from PDFs with Python, I have the following code:
import io
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
def extract_text_from_pdf(pdf_path):
resource_manager = PDFResourceManager()
fake_file_handle = io.StringIO()
converter = TextConverter(resource_manager, fake_file_handle)
page_interpreter = PDFPageInterpreter(resource_manager, converter)
with open(pdf_path, 'rb') as fh:
for page in PDFPage.get_pages(fh,
caching=True,
check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
# close open handles
converter.close()
fake_file_handle.close()
if text:
return text
if __name__ == '__main__':
path = '../_pdfs/mypdf.pdf'
print(extract_text_from_pdf(path))
This works (yay!), but what I really want to do is request the pdf directly, via its url, rather than open a pdf that has been pre-saved to a local drive.
I have no idea how I need to amend the "with open" logic to call from a remote url, nor am I sure which request library I would be best using for the latest version of Python (requests, urllib, urllib2, etc.?)
I'm new to Python, so please bear that in mind (P.s. I have found other questions on this, but nothing I can make work - possibly because they tend to be quite old.)
Any help would be greatly appreciated! Thank you!