Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以将正则表达式与pdfquery一起使用?_Python_Regex_Pdfminer - Fatal编程技术网

Python 是否可以将正则表达式与pdfquery一起使用?

Python 是否可以将正则表达式与pdfquery一起使用?,python,regex,pdfminer,Python,Regex,Pdfminer,我们是否可以使用正则表达式检测pdf中的文本(使用pdfquery或其他工具) 我知道我们可以做到: 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'))

我们是否可以使用正则表达式检测pdf中的文本(使用pdfquery或其他工具)

我知道我们可以做到:

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'
但我们需要这样的东西:

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'

这并不完全是正则表达式的查找,但它可以格式化/过滤可能的提取:

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)))
 ])
我没有测试下一个,但它也可能工作:

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>]
def some_regex_function_feature():
#这里你可以用一些正则表达式。
返回浮点(this.get('width',0))*浮点(this.get('height',0))>40000
pdf.pq('LTPage[page_index=“1”]*')。过滤器(此处为正则表达式函数过滤器)
[, ]