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
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"])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 Lanex0 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:
{'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.
PyMuPDF: words, and a search
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)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 LaneThe 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:
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())51-482913-6Positions 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:
{
"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.rotationbefore 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.