DIMITRIOS
DIMITRIOS

Reputation: 305

Extract full coordinates of words: PDFminer Python

I have a pdf and i want to highlight specified words with python.

I already have the tool (pypdf2) to highlight which to put it simple has a function with 4 arguments:

highlight(x1,y1,x2,y2)

x1,y1 : coordinates of bottom left corner(start of word) x2,y2 : coordinates of upper right corner(end of word)

This code of pdfminer that i found on the web provides the following output given a pdf.

For each word: (x1,y1,word).

I also need it to provide the x2,y2 coordinates if possible.

Pdfminer code link: https://code-examples.net/en/q/15d65e1

Same code with link below.

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( "C:/Users/koufo/PycharmProjects/untitled1/sample.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()

# 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):

    # loop over the object list
    for obj in lt_objs:

        # if it's a textbox, print text and location
        if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):
            print("%6d, %6d, %s" % (obj.bbox[0], obj.bbox[1], obj.get_text().replace('\n', '_')))

        # if it's a container, recurse
        elif isinstance(obj, pdfminer.layout.LTFigure):
            parse_obj(obj._objs)

# loop over all pages in the document
for page in PDFPage.create_pages(document):

    # read the page into a layout object
    interpreter.process_page(page)
    layout = device.get_result()

    # extract text from this object
    parse_obj(layout._objs)

Upvotes: 1

Views: 3892

Answers (1)

crissal
crissal

Reputation: 2647

x0, y0, x1, y1 = some_lobj.bbox

source

Upvotes: 2

Related Questions