Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use regular expressions with pdfquery?

Can we use regex to detect text within a pdf (using pdfquery or another tool)?

I know we can do this:

pdf = pdfquery.PDFQuery("tests/samples/IRS_1040A.pdf")
pdf.load()
label = pdf.pq('LTTextLineHorizontal:contains("Cash")')
left_corner = float(label.attr('x0'))
bottom_corner = float(label.attr('y0'))
cash = pdf.pq('LTTextLineHorizontal:in_bbox("%s, %s, %s, %s")' % \
        (left_corner, bottom_corner-30, \
        left_corner+150, bottom_corner)).text()
print cash
'179,000.00'

But we need something like this:

pdf = pdfquery.PDFQuery("tests/samples/IRS_1040A.pdf")
pdf.load()
label = pdf.pq('LTTextLineHorizontal:regex("\d{1,3}(?:,\d{3})*(?:\.\d{2})?")')
cash = str(label.attr('x0'))
print cash
'179,000.00'
like image 221
Dayvid Oliveira Avatar asked Oct 31 '22 17:10

Dayvid Oliveira


1 Answers

This is not exactly a lookup for a regex, but it works to format/filter the possible extractions:

def regex_function(pattern, match):
    re_obj = re.search(pattern, match)
    if re_obj != None and len(re_obj.groups()) > 0:
        return re_obj.group(1)
    return None

pdf = pdfquery.PDFQuery("tests/samples/IRS_1040A.pdf")

pattern = ''
pdf.extract( [
('with_parent','LTPage[pageid=1]'),
('with_formatter', 'text'),
('year', 'LTTextLineHorizontal:contains("Form 1040A (")', 
        lambda match: regex_function(SOME_PATTERN_HERE, match)))
 ])

I didn't test this next one, but it might work also:

def some_regex_function_feature():
    # here you could use some regex.
    return float(this.get('width',0)) * float(this.get('height',0)) > 40000

pdf.pq('LTPage[page_index="1"] *').filter(regex_function_filter_here)
[<LTTextBoxHorizontal>, <LTRect>, <LTRect>]
like image 181
Dayvid Oliveira Avatar answered Nov 08 '22 05:11

Dayvid Oliveira