-
-
Save 00krishna/5536dcc3d28aeb9cee87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Extract PDF text using PDFMiner. Adapted from | |
http://stackoverflow.com/questions/5725278/python-help-using-pdfminer-as-a-library | |
""" | |
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter#process_pdf | |
from pdfminer.pdfpage import PDFPage | |
from pdfminer.converter import TextConverter | |
from pdfminer.layout import LAParams | |
from cStringIO import StringIO | |
def pdf_to_text(pdfname): | |
# PDFMiner boilerplate | |
rsrcmgr = PDFResourceManager() | |
sio = StringIO() | |
codec = 'utf-8' | |
laparams = LAParams() | |
device = TextConverter(rsrcmgr, sio, codec=codec, laparams=laparams) | |
interpreter = PDFPageInterpreter(rsrcmgr, device) | |
# Extract text | |
fp = file(pdfname, 'rb') | |
for page in PDFPage.get_pages(fp): | |
interpreter.process_page(page) | |
fp.close() | |
# Get text from StringIO | |
text = sio.getvalue() | |
# Cleanup | |
device.close() | |
sio.close() | |
return text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment