Last active
March 30, 2023 03:07
-
-
Save jmcarp/7105045 to your computer and use it in GitHub Desktop.
Extract text from PDF document using PDFMiner
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 |
I'm so glad to announce that I released Python Multiple and Large PDF Documents Text Extraction solution on Github:
https://github.com/ahmedkhemiri95/PDFs-TextExtract
How do you extract a URL present in a PDF? For ex. if you are trying to extract the URL present in the left hand panel of the pdf version of a LinkedIn Profile using PDF miner?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code, I am using right now is:
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
import pdfminer
Open a PDF file.
fp = open('doc.pdf', 'rb')
Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
Create a PDF document object that stores the document structure.
Password for initialization as 2nd parameter
document = PDFDocument(parser)
Check if the document allows text extraction. If not, abort.
if not document.is_extractable:
raise PDFTextExtractionNotAllowed
Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager()
Create a PDF device object.
device = PDFDevice(rsrcmgr)
BEGIN LAYOUT ANALYSIS
Set parameters for analysis.
laparams = LAParams(all_texts=True)
Create a PDF page aggregator object.
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)
def parse_obj(lt_objs):
file = open('doc.txt', "a+")
loop over all pages in the document
for page in PDFPage.create_pages(document):