Tools,

Table Transformer, tested on 72 PDF tables

53 of 72

tables right, every cell, with v1.1

Get the script

Table Transformer is a set of free Microsoft models that find tables, rows and columns in page images. My script adds words.

Level with Docling on digital PDFs

The test: 72 made-up tables in PDFs with a text layer. A table counts only when every cell is right.

72 tables, 6,175 cells
Way Tables right Cells right
Table Transformer v1.1, PDF words 53 88.3%
Table Transformer v1.0, PDF words 14 81.1%
Table Transformer v1.1, Tesseract words 18 81.1%
Docling (its own TableFormer model) 52 88.5%
Our Actor's code 55 89.4%

It misses most on tables with no rule lines.

Tables right, by rule lines
Lines Table Transformer v1.1 Docling
Full grid 10 of 11 11 of 11
Some lines 20 of 25 22 of 25
No lines 23 of 36 19 of 36
Why v1.1 missed 19 tables
  • 10 tables: a column split in two, or two columns joined into one.
  • 4 tables: a cell that wraps onto a second line became an extra row (one of them also had a column split).
  • 5 tables: the shape was right, but some words landed in the wrong cell.

Use v1.1, not v1.0

On Hugging Face, the v1.0 structure model had over 8 times the downloads of v1.1-all in the last 30 days. In my test, v1.0 often found no row for the header line.

The top of one test table twice, with each found row outlined in red. The v1.0 panel has no box around the header line. The v1.1-all panel has one.
Rows each model found on a table with no lines. v1.0 skips the header.

The header row came out right on 29 of 72 tables with v1.0, and on 55 with v1.1-all.

The script: PDF tables to CSV

tatr2csv.py (MIT) writes one CSV file for each table. Add --ocr for a scan with no text layer. That needs the tesseract command.

pip install torch transformers timm pypdfium2 pdfplumber pillow
python tatr2csv.py report.pdf out/
python tatr2csv.py scan.pdf out/ --ocr

Each word goes to the row and the column it overlaps most. The first run downloads 2 models of about 115 MB each.

2 errors in transformers 5, and the fix

With transformers 5.23.0, the v1.1-all model stops with Field 'dilation' expected bool, got NoneType. Its config.json has "dilation": null. The script sets it to False before it loads the model.

Its image settings then fail with Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. The script passes the sizes from Microsoft's own inference.py: 800 pixels to find tables, 1000 for rows and columns.

Scans: the OCR is the weak link

The model reads images, so it works on scans. But the words then come from OCR, and one wrong character makes a cell wrong.

4 scanned tables with grid lines, 256 cells
Way Cells right Tables right
Table Transformer v1.1, Tesseract words 185 0
Our Actor's code 254 2

On the 72 clean pages, Tesseract's words cost it 35 tables. Grid lines came out as | or ], and "83 g" as "83g".

A vision model reads the picture whole: Granite Vision 4.1 on these tables.

Fast enough on a CPU

Median seconds a page, 10 PDFs, CPU only
Way Seconds
Table Transformer, all steps 0.25
Docling 0.9
Our Actor's code (no table model) 0.03

Finding the table took 0.09 seconds, and the rows and columns 0.12, on the CPU of an Apple M5 Max. Each run also takes about 2.8 seconds to start and load the models.

No install: our Actor reads it

Our Apify Actor reads a PDF in the cloud and returns each table as Markdown. Pages with their own text are free. A scanned page costs $0.006, and each run has a $0.00005 start fee at the default 1 GB.

Try the Actor

To turn its Markdown into CSV, use md2csv.py from PDF to CSV.

How I tested it
  • The 72 tables: the 72 new test tables of PDF to CSV, rebuilt today with the same truth. Every value is made up. The script's settings were fixed before its first run on them.
  • The models: table-transformer-detection, and the structure models v1.1-all and v1.0, at their Hugging Face revisions of November 21. Thresholds 0.5, pages drawn at 300 dpi.
  • Tesseract 5.5.4, English, default settings. The scans: 300 dpi, blur, gray noise and JPEG at quality 60.
  • Docling 2.143.1 with no OCR. Our Actor's code at commit 528fe416 (build 0.1.102), run on the same Mac. No Apify run.
  • torch 2.15.0, transformers 5.23.0, timm 1.0.31, Python 3.12, CPU only. Speed: 2 threads (our Actor's code: 1).
What I did not test

Microsoft's own inference.py and its cell rules, spanning cells, rotated tables, real documents, other OCR engines, a GPU, and camelot's flavor="ml", which also uses this model.

Questions people ask

Is Table Transformer free for commercial use?

The code and the 3 models I used are under the MIT license, which allows commercial use. Microsoft archived the GitHub repository, so expect no fixes.

Does Table Transformer read the text?

No. It returns boxes for tables, rows and columns. The words come from the PDF's text layer or from an OCR engine such as Tesseract.

Which structure model should I use?

table-transformer-structure-recognition-v1.1-all. In my test it got 53 of 72 tables exact. The older v1.0 model got 14, mostly because it lost header rows.