-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread-pdfs.py
37 lines (30 loc) · 1.08 KB
/
read-pdfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
__author__ = 'kalcho'
import requests
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO, BytesIO, open
def readpdf(pdf_file):
fd = StringIO(pdf_file.content)
print fd.encoding
rsrcmgr = PDFResourceManager()
retstr = StringIO()
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
pagenums = set()
for page in PDFPage.get_pages(fd, pagenums):
interpreter.process_page(page)
device.close()
content = retstr.getvalue()
retstr.close
fd.close()
return content
pdf_file = requests.get("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
# if file downloaded locally, uncomment next line, and comment previous line
#pdf_file = open("pages/warandpeace/chapter1.pdf")
output_string = readpdf(pdf_file)
print output_string
pdf_file.close()