【问题标题】:Using python PyMuPDF (fitz) to iterate through lines and check length of it and add a period if it meets the criteria使用 python PyMuPDF (fitz) 遍历行并检查其长度,如果符合条件则添加句点
【发布时间】:2020-08-27 20:06:34
【问题描述】:

尝试从 PyMuPDF 库中遍历页面的每一行以检查句子的长度,如果少于 10 个单词,那么我想添加一个句号。 伪代码是:

#loop through the lines of the PDF
#check number of words in line
#if line has less than 10 words 
#add period 

下面的真实代码:

import fitz
myfile = "my.pdf"
doc  =fitz.open(myfile)
page=doc[0]
for page in doc:
    text = page.getText("text")
    print(text)

当我添加另一个 for 循环时,例如 for line in page:

我收到一条错误消息,提示页面不可迭代。有没有其他方法可以做到这一点?

谢谢

【问题讨论】:

标签: python pymupdf


【解决方案1】:

为了遍历页面行,您可以使用 getDisplayList:

page_display = page.getDisplayList()
dictionary_elements = page_display.getTextPage().extractDICT()
for block in dictionary_elements['blocks']:
    for line in block['lines']:
        line_text = ''
        for span in line['spans']:
             line_text += ' ' + span['text]
        print(l

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2019-02-02
    相关资源
    最近更新 更多