Извлечь текст в прямоугольник из pdf - Python - PullRequest
0 голосов
/ 13 февраля 2020

У меня есть требование, чтобы извлечь текст, который в прямоугольнике из PDF. Есть несколько методов, которые я протестировал. Но не получаю указанный c текст. Например, я тестировал пакеты PyMuPDF, pdfplumber, tabula, camelot, pdftables. В модуле PyMuPDF он запрашивает начальные и конечные слова для извлечения текста. Как я понимаю, остальные пакеты также просто извлекают линии, информацию о кривых, но не текст.

Я хочу получить текст из прямоугольников в PDF без указания начального и конечного текста.

https://drive.google.com/file/d/1wCvik7VbEvDwbT-mapgXc8fwlq7Ao3BP/view?usp=sharing

1 Ответ

0 голосов
/ 13 февраля 2020

Вы можете использовать код ниже

import PyPDF2
def convert_pdf_to_text (document):
    read_pdf = PyPDF2.PdfFileReader(document, strict=False)
    number_of_pages = read_pdf.getNumPages()

    alltext1=""
    for page_number in range(number_of_pages):
        page = read_pdf.getPage(page_number)
        alltext1 += page.extractText()
    return alltext1.replace("\n", "")
convert_pdf_to_text ('pdf_test.pdf')

Выход

'A Simple PDF File  This is a small demonstration .pdf file - just for use in the Virtual Mechanics tutorials. More text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. Boring, zzzzz. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. And more text. Even more. Continued on page 2 ...  Details  State: State_name     City: City_name    Country: Country_name     Rig No: 4455555  Source Id: k4-3k44 '
...