gpt4 book ai didi

python - 单个单词的 PDFMiner 提取 - LTText LTTextBox

转载 作者:行者123 更新时间:2023-12-04 02:41:31 26 4
gpt4 key购买 nike

在下面的示例中,我使用 PDFMiner 生成单词 x,y 坐标,但是结果是逐行生成的。我怎样才能将每个单词从另一个单词中拆分出来,而不是逐行拆分单词组(参见下面的示例)。我已经尝试了 PDFMiner tutorial 中的几个论点。 . LTTextBoxLTText 都试过了。此外,我不能使用文本分析中通常使用的开始和结束偏移量。

这个 PDF 是一个很好的例子,它用在下面的代码中。

http://www.africau.edu/images/default/sample.pdf

from pdfminer.layout import LAParams, LTTextBox, LTText
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
from pdfminer.converter import PDFPageAggregator

#Imports Searchable PDFs and prints x,y coordinates
fp = open('C:\sample.pdf', 'rb')
manager = PDFResourceManager()
laparams = LAParams()
dev = PDFPageAggregator(manager, laparams=laparams)
interpreter = PDFPageInterpreter(manager, dev)
pages = PDFPage.get_pages(fp)

for page in pages:
print('--- Processing ---')
interpreter.process_page(page)
layout = dev.get_result()
for lobj in layout:
if isinstance(lobj, LTText):
x, y, text = lobj.bbox[0], lobj.bbox[3], lobj.get_text()
print('At %r is text: %s' % ((x, y), text))

这将返回可搜索 PDF 的 x,y 坐标,如下所示:

--- Processing ---
At (57.375, 747.903) is text: A Simple PDF File
At (69.25, 698.098) is text: This is a small demonstration .pdf file -
At (69.25, 674.194) is text: just for use in the Virtual Mechanics tutorials. More text. And more
text. And more text. And more text. And more text.

想要的结果(坐标为演示的代理):

--- Processing ---
At (57.375, 747.903) is text: A
At (69.25, 698.098) is text: Simple
At (69.25, 674.194) is text: PDF
At (69.25, 638.338) is text: File

最佳答案

使用 PDFMiner,在遍历每一行之后(就像你已经做的那样),你只能遍历行中的每个字符。

我用下面的代码做到了这一点,同时尝试记录每个单词第一个字符的 x、y 并设置条件以在每个 LTAnno 处拆分单词(例如\n )或.get_text() == ' ' 空格。

from pdfminer.layout import LAParams, LTTextBox, LTText, LTChar, LTAnno
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
from pdfminer.converter import PDFPageAggregator

#Imports Searchable PDFs and prints x,y coordinates
fp = open('C:\sample.pdf', 'rb')
manager = PDFResourceManager()
laparams = LAParams()
dev = PDFPageAggregator(manager, laparams=laparams)
interpreter = PDFPageInterpreter(manager, dev)
pages = PDFPage.get_pages(fp)

for page in pages:
print('--- Processing ---')
interpreter.process_page(page)
layout = dev.get_result()
x, y, text = -1, -1, ''
for textbox in layout:
if isinstance(textbox, LTText):
for line in textbox:
for char in line:
# If the char is a line-break or an empty space, the word is complete
if isinstance(char, LTAnno) or char.get_text() == ' ':
if x != -1:
print('At %r is text: %s' % ((x, y), text))
x, y, text = -1, -1, ''
elif isinstance(char, LTChar):
text += char.get_text()
if x == -1:
x, y, = char.bbox[0], char.bbox[3]
# If the last symbol in the PDF was neither an empty space nor a LTAnno, print the word here
if x != -1:
print('At %r is text: %s' % ((x, y), text))

输出如下所示

At (64.881, 747.903) is text: A
At (90.396, 747.903) is text: Simple
At (180.414, 747.903) is text: PDF
At (241.92, 747.903) is text: File

或许您可以根据您的要求和喜好优化检测词的条件。 (例如,在词尾剪掉标点符号 .!?)

关于python - 单个单词的 PDFMiner 提取 - LTText LTTextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59182694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com