Tesseract layout: keep the spaces, indents and columns
16/16
form columns lined up by a TSV rebuild
By default, Tesseract puts one space between words, whatever the gap. Rebuilt from TSV, 117 of 122 indented lines came back right.
Which setting keeps the layout
| Setting | Form columns | Table columns | Indents | Words in order |
|---|---|---|---|---|
| psm 3, the default | 0/16 | 0/16 | 13/122 | 60.4% |
| psm 3 + flag | 0/16 | 0/16 | 13/122 | 60.4% |
| psm 4 | 4/16 | 4/16 | 13/122 | 98.5% |
| psm 4 + flag | 4/16 | 4/16 | 13/122 | 98.5% |
| psm 6 | 4/16 | 4/16 | 13/122 | 98.5% |
| psm 6 + flag | 4/16 | 4/16 | 13/122 | 98.5% |
| TSV rebuild | 16/16 | 12/16 | 117/122 | 98.5% |
| OCRmyPDF, then pdftotext -layout | 16/16 | 13/16 | 119/122 | 97.7% |
Flag: -c preserve_interword_spaces=1. It changed no score
here.
These pages are straight. On tilted copies the TSV rebuild kept its
columns and indents, and OCRmyPDF needed --deskew.
The one flag
On the command line:
tesseract scan.png stdout --psm 6 -c preserve_interword_spaces=1
In Python:
import pytesseract
from PIL import Image
config = "--psm 6 -c preserve_interword_spaces=1"
text = pytesseract.image_to_string(Image.open("scan.png"), config=config)
print(text)
Tesseract's own note for it reads "Preserve multiple interword spaces". It is off by default.
It keeps the gaps, not the columns. On one test form, a column's values started anywhere from character 28 to 37. On the indented pages, no indent survived.
More on config strings: pytesseract config options.
Rebuild the layout from TSV
TSV output gives each word's left edge in pixels. Divide by the width of one letter and you get its character column, so the gaps and indents come back.
The layout() function below is 26 lines of standard
library Python (3.10 or later). It reads Tesseract's TSV from
--psm 6.
Show the code
import csv
import io
import statistics
import subprocess
from itertools import pairwise
def layout(tsv):
"""Tesseract TSV to text that keeps the layout: each word at character column left / cw."""
rows = csv.DictReader(io.StringIO(tsv), delimiter="\t", quoting=csv.QUOTE_NONE)
words = [(int(w["top"]), int(w["left"]), int(w["width"]), w["text"], (w["block_num"], w["par_num"], w["line_num"]))
for w in rows if (w["text"] or "").strip()] # fmt: skip
if not words:
return ""
sizes = [wd / len(t) for _, _, wd, t, _ in words if len(t) >= 4] or [wd / len(t) for _, _, wd, t, _ in words]
cw = statistics.median(sizes) # pixels per character, from words of 4 or more letters
x0 = min(x for _, x, _, _, _ in words)
lines = {}
for w in words:
lines.setdefault(w[4], []).append(w)
lines = sorted((min(ws)[0], sorted(ws, key=lambda w: w[1])) for ws in lines.values())
pitch = max(1, statistics.median([b[0] - a[0] for a, b in pairwise(lines)] or [1]))
out, last = [], None
for top, ws in lines:
if last is not None and top - last > 1.5 * pitch:
out += [""] * (round((top - last) / pitch) - 1) # a big gap: blank lines
s, end = "", 0
for _, x, wd, t, _ in ws:
col = len(s) + 1 if s and x - end < 2 * cw else round((x - x0) / cw) # close to the word before: 1 space
s, end = s.ljust(max(col, len(s) + 1 if s else 0)) + t, x + wd
out.append(s)
last = top
return "\n".join(out)
tsv = subprocess.run(
["tesseract", "scan.png", "stdout", "--psm", "6", "tsv"], capture_output=True, text=True, check=True
).stdout
print(layout(tsv))
- Tilted half a degree, it still kept every form column and 115 of 122 indented lines, but exact spacing fell to 34 lines.
- Right-aligned amounts can drift a character or two: digits are wider, and commas and points narrower, than an average letter.
- On the indented pages, spacing came out exactly right on 92 of the 110 lines the OCR read right.
Or OCRmyPDF, then pdftotext -layout
OCRmyPDF adds an invisible text layer, each word at its place on the
page. pdftotext -layout then lines the words up by
position.
ocrmypdf -l eng --deskew scan.pdf searchable.pdf
pdftotext -layout searchable.pdf out.txt
Starting from a PNG with a dpi tag? OCRmyPDF takes it directly.
-
Tilted half a degree, without
--deskew: rows split in two, 4 of 16 table columns, 86.7% of words in order. - With
--deskew: 13 of 16, and 96.7% of words. - OCRmyPDF lost lone digits: on one straight invoice, only 5 of 13 Qty cells came through.
psm 3, 4 or 6
psm 3, the default, looks for columns and reads them one at a time. psm 4 and 6 read each line straight across: Tesseract and two columns.
- On the forms, psm 3 wrote a column of labels, then the column of their values, then the next pair of columns.
- psm 4 and 6 tied on every form and table score. psm 6 was one line better on the indented pages.
Use psm 6 or 4 for forms and tables. All the modes: Tesseract psm modes.
If you want Markdown tables, not spaces
PDF OCR to Markdown, our Apify Actor, writes a table as a Markdown table.
- Tables: 314 of 316 cells right.
- Forms: the left pairs came out right, but a second pair on the same row fell out of its row.
- Indents: lost. Lines were joined into paragraphs.
Pick it for tables, not for indents. Tested: the code of build 0.1.125, on our Mac.
- Scanned pages: $0.006 each. Digital pages: free. Plus a $0.00005 start fee a run at the default 1 GB.
- On Apify's free plan, it stops at 20 pages a run and 50 a month.
What I did not test
- Real scans of forms, tables or code. These were made up.
- Languages other than English, or models other than tessdata_fast.
- Phone photos, pages under 300 dpi, and any tilt but 0.5 degrees.
- Indents in a proportional font: every indented page was monospace.
- Other layouts: the 4 forms share one set of columns, the tables two.
- Tables with grid lines, and book pages in two columns.
- hOCR and ALTO output, and psm 11 and 12.
-
--deskewon straight pages, and OCRmyPDF at psm 6. - Windows and Linux. Everything ran on one Mac.
- The Actor on Apify itself, or on tilted pages.
How I tested it
- 12 made-up pages, Letter at 300 dpi: 4 forms, 4 tables without grid lines (bank statements and invoices), and 4 indented pages (YAML, Python, an outline, an nginx config). Plus a copy of each tilted 0.5 degrees, blurred, with noise.
- 10 proofread book pages, 1,955 words: every setting in the table found 96.0% of them in order.
- A column counts as kept when at least half its words (and 2 or more) are found, and each starts (for amounts, ends) within 1 character of the column's median. So a kept column can spread over 3 characters. Each page has 4, one of them the left margin. An indented line counts when it sits at its level's column.
- Words in order: the true words found in order, of 1,099.
- Tesseract 5.5.4 with eng from tessdata_fast 4.1.0, pytesseract 0.3.13, Pillow 13.0.0, OCRmyPDF 17.13.0, pdftotext 26.03.0 (Poppler), Python 3.13.11, macOS 26.6.
- Scripts and results are in research/bench/tesseract-layout-2026-11-22/.
Sources
- Tesseract docs: page segmentation modes
- Tesseract docs: command line usage
- pytesseract on PyPI
- OCRmyPDF docs
- Poppler
- We make the Apify Actor, and none of the other tools here.
Questions people ask
How do I keep spaces in Tesseract output?
Add -c preserve_interword_spaces=1. Without it,
Tesseract writes one space between words, however wide the gap. It
keeps gaps, not columns: at psm 4 or 6 only 4 of 16 form columns
lined up, the left margins, as without it. At the default psm 3:
0.
Can Tesseract keep the layout of a page?
Partly. Its own text kept no indents here, and only a form's left margin. Rebuilt from TSV at psm 6, 16 of 16 form columns and 117 of 122 indented lines came out right. Right-aligned numbers drifted a character or two.
Does Tesseract keep indents?
Not in its own text. On our indented pages no line started with a
space, in psm 3, 4 or 6, with or without
preserve_interword_spaces=1: 13 of 122 lines right,
all at the left margin. The TSV rebuild got 117.
Which psm keeps a form's columns lined up?
None alone. psm 4 and 6 keep each row on one line, but only the left margin lined up: 4 of 16 columns, flag or not. psm 3, the default, kept 0. Use psm 6 with the TSV rebuild.
Related guides
- Tesseract psm modes: which one to use, tested (2026)
- Tesseract two columns: psm 3 vs 4 vs 6 on real scans
- pytesseract config options, tested: the mistakes it hides
- Tesseract bounding box coordinates, tested in Python
- Tesseract table extraction, tested on 4,575 cells
- OCRmyPDF output to text: --sidecar, tested