使用 PDFminer 从发票 PDF 中提取特定数据值:Python

问题描述

从具有不同结构的多个 PDF 中仅提取特定数据,

我已将所有 pdf 存储到发票文件夹中。 我尝试使用 pdfminer 库从 pdf 中提取数据。

def extract_text(pdf_path):
    text21 = ''
    for page in extract_text_by_page(pdf_path):
        text21 = text21 + str(page[:-1]) + ' '
    return text21


inv = glob.glob(path+"/Invoice/*.pdf")

for i in inv:
    print(i)
    page = extract_text(i)
    print(page)
    data1 = str(page)
    lan = len(data1)

     x = re.search("Invoice Number:",page)
    x1 = re.search("Invoice No:",page)
    x2 = re.search("Bill No:",page)
    x3 = re.search("Bill:",page)
    if (x or x1 or x2 or x3):
        if x:
            yo = x.end()
        elif x1:
            yo = x1.end()
        elif x2:
            yo = x2.end()
        elif x3:
            yo = x3.end()

同样希望从发票 PDF 文件中附加发票日期、总账单金额。我如何将所有值附加到单独的变量中作为提取值供其他进程使用。

解决方法

使用pText

with open("input.pdf","rb") as pdf_file_handle:
    l = RegularExpressionTextExtraction("Invoice Number :[0-9]+")
    doc = PDF.loads(pdf_file_handle,[l])

# do something with these events
l.get_matched_text_render_info_events_per_page(0)