簡體   English   中英

如何使用pdfplumber提取的文本在Python中打印下一行

[英]How to print the next line in Python with text extracted using pdfplumber

如何使用 pdfPlumber extract.text 函數從我從 PDF 中提取的文本中打印下一行?

我試過 line.next() 但它不起作用。

實際作業名稱在“作業名稱”之后的行上。 按照下面的例子。

職位名稱

奧爾巴尼購物中心發展

我的代碼如下。

jobName_re = re.compile(r'(Job Name)')
siteAddress_re = re.compile(r'(Wellington\s)(.+)')
file = 'invoices.pdf'

lines = []

with pdfplumber.open(file) as myPdf:
    for page in myPdf.pages:
        text = page.extract_text()
        for line in text.split('\n'):
            jobName = jobName_re.search(line)
            siteAddress = siteAddress_re.search(line)
            if jobName:
                print('The next line that follows Job Name is', line.next())
            elif siteAddress:
                print(siteAddress.group(1))

您有多種選擇。

選項1

您可以切換到使用整數索引來循環記錄:

lines = text.split('\n')
for i in range(len(lines)):
    line = lines[i]

然后您可以訪問lines[i+1]

選項 2

設置一個標志,表示您已經看到作業名稱的標題,然后在下一次循環時選擇它。 像這樣的東西:

        last_was_job_heading = False
        for line in text.split('\n'):
            siteAddress = siteAddress_re.search(line)
            if last_was_job_heading:
                print('The next line that follows Job Name is', line)
            elif siteAddress:
                print(siteAddress.group(1))
            last_was_job_heading = jobName_re.search(line)

選項 3

根本不要將文本分成幾行。 而是使用更智能的正則表達式一次解析多行。

選項 4

使用某種解析庫而不是正則表達式。 在這種簡單的情況下,這可能有點矯枉過正。

暫無
暫無

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

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