How can I extract text fragments from PDF with their coordinates in Python?

Martin Thoma :

Given a digitally created PDF file, I would like to extract the text with the coordinates. A bounding box would be awesome, but an anchor + font / font size would work as well.

I've created an example PDF document so that it's easy to try things out / share the result.

What I've tried

pdftotext

pdftotext PDF-export-example.pdf -layout

gives this output. It already contains the text, but the coordinates are not in there.

PyPDF2

PyPDF2 is even worse - also neither coordinates, nor font size and in this case not even ASCII art clues how the layout was:

from PyPDF2 import PdfFileReader


def text_extractor(path):
    with open(path, "rb") as f:
        pdf = PdfFileReader(f)
        page = pdf.getPage(0)
        text = page.extractText()
        print(text)


if __name__ == "__main__":
    path = "PDF-export-example.pdf"
    text_extractor(path)

pdfminer.six

Another method to extract text, but without coordinates / font size. Thank you to Duck puncher for this one:

from io import StringIO

from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
from pdfminer.pdfpage import PDFPage


def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = "utf-8"
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = open(path, "rb")
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos = set()

    for page in PDFPage.get_pages(
        fp,
        pagenos,
        maxpages=maxpages,
        password=password,
        caching=caching,
        check_extractable=True,
    ):
        interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text


if __name__ == "__main__":
    print(convert_pdf_to_txt("PDF-export-example.pdf"))

This one goes a bit more in the right direction as it can give the font name and size. But the coordinates are still missing (and the output is a bit verbose as it is character-by-character):

from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer, LTChar

for page_layout in extract_pages("PDF-export-example.pdf"):
    for element in page_layout:
        if isinstance(element, LTTextContainer):
            for text_line in element:
                for character in text_line:
                    if isinstance(character, LTChar):
                        print(character)
                        print(character.fontname)
                        print(character.size)

tabula-py

Here I don't get anything at all:

from tabula import read_pdf

df = read_pdf("PDF-export-example.pdf")
print(df)
Vishal Singh :

I've used PyMuPDF to extract page content as a list of single words with bbox information.

import fitz

doc = fitz.open("PDF-export-example.pdf")

for page in doc:
    wlist = page.getTextWords()  # make the word list
    print(wlist)

Output:

[
    (72.0250015258789, 72.119873046875, 114.96617889404297, 106.299560546875, 'Test', 0, 0, 0),
    (120.26901245117188, 72.119873046875, 231.72618103027344, 106.299560546875, 'document', 0, 0, 1),
    (72.0250015258789, 106.21942138671875, 101.52294921875, 120.18524169921875, 'Lorem', 1, 0, 0),
    (103.98699951171875, 106.21942138671875, 132.00445556640625, 120.18524169921875, 'ipsum', 1, 0, 1),
    (134.45799255371094, 106.21942138671875, 159.06637573242188, 120.18524169921875, 'dolor', 1, 0, 2),
    (161.40098571777344, 106.21942138671875, 171.95208740234375, 120.18524169921875, 'sit', 1, 0, 3),
    ...
]

page.getTextWords()

  • method separates a page’s text into “words” using spaces and line breaks as delimiters. Therefore the words in this lists contain no spaces or line breaks.

  • Return type: list

An item of this list looks like this:

(x0, y0, x1, y1, "word", block_no, line_no, word_no)

Where the first 4 items are the float coordinates of the words’s bbox. The last three integers provide some more information on the word’s whereabouts.


A Note on the Name fitz
The standard Python import statement for PyMuPDF library is import fitz. This has a historical reason:

The original rendering library for MuPDF was called Libart.

After Artifex Software acquired the MuPDF project, the development focus shifted on writing a new modern graphics library called Fitz. Fitz was originally intended as an R&D project to replace the aging Ghostscript graphics library, but has instead become the rendering engine powering MuPDF.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HOW can i extract text from SPAN? Please

How to extract table as text from the PDF using Python?

How to extract text and text coordinates from a PDF file?

How can I extract text from the middle of a string with Javascript?

How can I extract address from raw text using NLTK in python?

How extract extract specific text from pdf file - python

Extract text coordinates from iframe

How to extract text from pdf in Python 3.7

How can I extract text from string in python?

How to extract text from PDF?

Extract table from PDF with coordinates

How can I use python to write a dxf file from coordinates?

How can I extract text from images?

How can I highlight and extract text with links to within a PDF?

How can I save coordinates to a text file?

How can I extract pdf names from an lftp log file?

How can I extract a PDF/SWF file from an EXE?

How do I extract text fragments of a file using sed?

How can I extract only certain text from similar elements using BeautifulSoup and Python

How can I extract a text from a bytes file using python

How can we extract the specific value from pdf using python?

How can I extract number from text with regular expression

How can I extract the text from the <em> tag using BeautifulSoup

How can I extract the maximum values along the horizontal and vertical from the coordinates of the grid using python?

How can i extract text from a PDF with python?

How to extract Bold text from pdf using python?

How Can I Create Variables From Text and Convert Coordinates

How to extract text from PDF files and save as CSV using python

How can I extract color of image from PDF IText Java?

TOP Ranking

HotTag

Archive