我想从PDF中提取文本。我从文本提取中得到的输出没有那么有条理
PDF链接(仅第1页):https://microprecision.com/wp-content/uploads/2020/08/Sample-Cert_rev-7-1.pdf
我想提取参数,如MPC控制编号、序列号、型号等,并将它们作为键值对存储在字典中
我正在尝试下面的代码,但没有得到想要的输出
import io
from pdfminer.layout import LAParams, LTTextBox
from pdfminer3.pdfpage import PDFPage
from pdfminer3.pdfinterp import PDFResourceManager,PDFPageInterpreter
from pdfminer3.converter import PDFPageAggregator,TextConverter
def pdftotext(path):
resource_manager = PDFResourceManager()
file_handle = io.StringIO()
laprams = LAParams(word_margin=1.0,boxes_flow=0.5,char_margin=2.0,line_overlap=0.5,line_margin=0.5)
converter = TextConverter(resource_manager,file_handle, laparams=laprams)
page_interpreter = PDFPageInterpreter(resource_manager,converter)
i = 1
with open(path,'rb') as fh:
for page in PDFPage.get_pages(fh,caching=False,check_extractable=True):
page_interpreter.process_page(page)
text = file_handle.getvalue()
converter.close()
file_handle.close()
return text
raw = pdftotext('Sample-Certificate.pdf')
print(raw)
实际上,您没有得到键值对,而且
pdfminer
根本无法向您提供它。它只会从PDF中提取文本(+可能还有一些附加信息)要获得好的逻辑标签-值对,需要在提取的文本上使用Information Extraction方法和/或Named Entity Recognition。这里有很多选择。您可能想先看看SpaCy或NLTK
通常,从文档中提取有意义的数据及其关系具有一个新的性感名称Document Intelligence
使用PDF文件时,我更喜欢使用PyMuPDF库https://pypi.org/project/PyMuPDF/
注意如何正确安装库
以下是输出:
相关问题 更多 >
编程相关推荐