[英]How to extract only specific text from PDF file using python
如何使用python僅從PDF文件中提取某些特定文本並將輸出數據存儲到Excel的特定列中。
這是示例輸入 PDF 文件 (File.pdf)
鏈接到完整的 PDF 文件File.pdf
我們需要從整個 PDF 文件中提取Invoice Number、Due Date 和 Total Due的值。
到目前為止我使用過的腳本:
from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
output_string = StringIO()
with open('file.pdf', 'rb') as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
print(output_string.getvalue())
但沒有從 PDF 文件中獲取特定的輸出值。
如果您想以自己的方式(pdfminer)查找數據,您可以搜索一種模式來提取數據,如下所示(新的是末尾的正則表達式,基於您給定的數據):
from io import StringIO
import re
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
output_string = StringIO()
with open('testfile.pdf', 'rb') as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
finding = re.search(r"INV-\d+\n\d+\n.+\n.+\n\$\d+\.\d+", output_string.getvalue())
invoice_no, order_no, _, due_date, total_due = finding.group(0).split("\n")
print(invoice_no, order_no, due_date, total_due)
如果您想將數據存儲在 excel 中,您可能需要更具體(或打開一個新問題)或查看這些頁面:
https://www.geeksforgeeks.org/writing-excel-sheet-using-python/
https://xlsxwriter.readthedocs.io/
PS:另一個答案看起來是一個很好的解決方案,你只需要過濾數據
編輯:第二個解決方案。 在這里,我使用另一個包PyPDF2
,因為在那里您以其他順序獲取數據(也許 PDFMiner 也可以這樣做)。 如果值之前的文本始終相同,您可以找到這樣的數據:
import re
import PyPDF2
def parse_pdf() -> list:
with open("testfile.pdf", "rb") as file:
fr = PyPDF2.PdfFileReader(file)
data = fr.getPage(0).extractText()
regex_invoice_no = re.compile(r"Invoice Number\s*(INV-\d+)")
regex_order_no = re.compile(r"Order Number(\d+)")
regex_invoice_date = re.compile(r"Invoice Date(\S+ \d{1,2}, \d{4})")
regex_due_date = re.compile(r"Due Date(\S+ \d{1,2}, \d{4})")
regex_total_due = re.compile(r"Total Due(\$\d+\.\d{1,2})")
invoice_no = re.search(regex_invoice_no, data).group(1)
order_no = re.search(regex_order_no, data).group(1)
invoice_date = re.search(regex_invoice_date, data).group(1)
due_date = re.search(regex_due_date, data).group(1)
total_due = re.search(regex_total_due, data).group(1)
return [invoice_no, due_date, total_due]
if __name__ == '__main__':
print(parse_pdf())
也許您必須更改正則表達式,因為它們僅基於給定的示例。 正則表達式僅在找到正則表達式時才有效,因此您必須使用try: except
per regex ;)
如果這不能回答您的問題,您必須提供更多信息/示例 pdf。
您可以使用 tabula 提取數據,並使用該數據可以使用 python 創建一個 excel 文件:
df = ("./Downloads/folder/myfile.pdf")
output = "./Downloads/folder/test.csv"
tabula.convert_into(df, output, output_format="csv", stream=True)
excel文件創建: https : //www.geeksforgeeks.org/python-create-and-write-on-excel-file-using-xlsxwriter-module/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.