How to get text with its coordinates from a PDF

Updated · Tested with Python 3.13, pdfplumber 0.11.11, PyMuPDF 2.2

In short

Use pdfplumber's extract_words() or PyMuPDF's get_text("words"). Both return each word with the edges of its box in points, measured from the top left of the page. Inside the PDF the origin is the bottom left, so convert with page height minus y when you mix sources. One point is 1/72 of an inch.

A PDF stores where every piece of text sits. Once you have those positions you can find a value by its place on the page: the text to the right of a label, the last number in a column, everything inside a box. The examples use company-extract.pdf.

How PDF coordinates work

  • The unit is the point: 1/72 of an inch. An A4 page is 595 by 842 points.
  • Inside the file, the origin is the bottom left corner and y grows upward.
  • Most libraries turn that around for you and measure from the top left, with y growing downward, as on a screen.

To convert between the two: y_from_top = page_height - y_from_bottom.

pdfplumber: words and characters

Python
import pdfplumber

with pdfplumber.open("company-extract.pdf") as pdf:
    page = pdf.pages[0]
    print(page.width, page.height)
    for word in page.extract_words()[2:6]:
        print(round(word["x0"], 1), round(word["top"], 1),
              round(word["x1"], 1), round(word["bottom"], 1), word["text"])
Output
595.275591 841.889764
68.4 133.0 117.6 144.0 Company
120.5 133.0 149.9 144.0 name
204.4 133.0 241.3 144.0 Harbor
244.1 133.0 269.0 144.0 Lane

x0 and x1 are the left and right edges, top and bottom are measured from the top of the page. page.chars gives the same for every single character, with its font and size:

Output
{'fontname': 'HUHQIP+Noto-Sans', 'x0': 75.31, 'y0': 697.88, 'x1': 81.97, 'y1': 708.88,
 'size': 11.0, 'text': 'o', 'top': 133.01, 'bottom': 144.01}

pdfplumber keeps both systems: y0 and y1 are from the bottom, top and bottom from the top.

Python
import pymupdf

with pymupdf.open("company-extract.pdf") as doc:
    page = doc[0]
    for x0, y0, x1, y1, word, *_ in page.get_text("words")[2:6]:
        print(round(x0, 1), round(y0, 1), round(x1, 1), round(y1, 1), word)
Output
68.4 129.0 117.6 144.0 Company
120.5 129.0 149.9 144.0 name
204.4 129.0 241.3 144.0 Harbor
244.1 129.0 269.0 144.0 Lane

The left and right edges match pdfplumber's. The top differs by four points, because each library draws the box around a word a little differently. Never compare positions from two libraries without a tolerance.

PyMuPDF can also search for text and return where it is, which makes "the value to the right of this label" a few lines:

Python
import pymupdf

with pymupdf.open("company-extract.pdf") as doc:
    page = doc[0]
    label = page.search_for("Company number")[0]
    right_of_label = pymupdf.Rect(label.x1, label.y0 - 2, page.rect.width, label.y1 + 2)
    print(page.get_text("text", clip=right_of_label).strip())
Output
51-482913-6

Positions and 100sands

100sands does not return positions. It has no character, word or box coordinates, and no fonts or colours: its /v1/text call returns the text of each page in reading order, and its document types return named values. For boxes, use pdfplumber or PyMuPDF as above.

What it does keep from the layout is the gap on a line. Where a label and its value are set apart, they come back separated by a tab, so Company name and Harbor Lane Logistics Ltd arrive as one line with a tab between them. The response for company-extract.pdf has this shape:

JSON
{
  "pages": 1,
  "text": "COMPANY EXTRACT\nCompany name\tHarbor Lane Logistics Ltd\nCompany number\t51-482913-6\n..."
}

Splitting each line at the tab pairs labels with values without any coordinates. If what you want is a value next to a label on documents of one kind, a document type returns it by name.

Things that go wrong

  • Rotated pages. A page can carry a rotation of 90 degrees. Check page.rotation before trusting x and y.
  • Different page sizes. Positions are in points, not percentages. A value at x = 204 on A4 sits elsewhere on US Letter.
  • Scans. A scanned page has no positions until OCR supplies them, and those are approximate.
  • Small shifts. The same form printed by two systems can differ by a few points. Search for a label and measure from it, as above, instead of using fixed numbers.

Questions

What units are PDF coordinates in?

Points. One point is 1/72 of an inch, so an A4 page is about 595 by 842 points and a US Letter page is 612 by 792.

Where is the origin of a PDF page?

In the file, the bottom left corner, with y growing upward. pdfplumber and PyMuPDF report positions from the top left, with y growing downward. Convert with y_from_top = page_height - y_from_bottom.

How do I find the value next to a label in a PDF?

Find the label's box, then read the text inside a rectangle that starts at the label's right edge and runs to the edge of the page at the same height. In PyMuPDF that is page.search_for(label) followed by page.get_text with a clip rectangle.

Do scanned PDFs have coordinates?

Not until OCR is run. OCR returns a position for each word it reads, but those are estimates from the picture, so allow a few points of tolerance.

Try it on your own documents

A free plan every month, no credit card. See the PDF to text API.

Start free