簡體   English   中英

使用 python PyMuPDF (fitz) 遍歷行並檢查它的長度,如果滿足條件則添加一個句點

[英]Using python PyMuPDF (fitz) to iterate through lines and check length of it and add a period if it meets the criteria

嘗試遍歷 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 line in page:添加另一個 for 循環例如for line in page:

我收到一條錯誤消息,說頁面不可迭代。 有沒有其他方法可以做到這一點?

謝謝

為了遍歷頁面行,您可以使用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM