How to extract tables from a PDF in Python

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

In short

A PDF stores no tables, only text at positions, so the tool has to infer rows and columns. For tables with drawn borders, pdfplumber's and PyMuPDF's defaults work. For tables whose columns are separated only by space, use PyMuPDF's find_tables(strategy="text") or pdfplumber's text strategy on a cropped area, and check that every row has the same number of cells.

A PDF has no tables. It has text at positions, and sometimes lines drawn between them. A table extractor has to guess the rows and columns from those, and the guess depends on how the table is drawn. The examples use invoice.pdf, whose table has four columns, thin lines under each row, and no lines between the columns.

Why the default often returns nothing

Python
import pdfplumber

with pdfplumber.open("invoice.pdf") as pdf:
    page = pdf.pages[0]
    print(page.extract_tables())
Output
[]

pdfplumber's default looks for cells boxed in by drawn lines on all sides. This table has none between its columns, so nothing is found. There are two families of tables, and each needs its own approach:

Kind of tableHow to find it
Ruled: every cell has bordersFollow the drawn lines (the default in most tools)
Unruled: columns are separated only by spaceUse the alignment of the text itself

pdfplumber with the text strategy

Tell pdfplumber to work out rows and columns from where the words line up, and give it only the part of the page that holds the table. Without the crop, it tries to turn the whole page into one table.

Python
import pdfplumber

with pdfplumber.open("invoice.pdf") as pdf:
    page = pdf.pages[0]
    top = page.search("Description")[0]["top"]
    bottom = page.search("Subtotal")[0]["top"]
    area = page.crop((0, top - 2, page.width, bottom - 2))
    table = area.extract_table({"vertical_strategy": "text", "horizontal_strategy": "text"})

for row in table:
    print(row)
Output
['Description', 'Quantity Unit', 'price', 'Amount']
['', '', '', '']
['A4 copy paper, 80 gsm, box of 5 reams', '40', '18.50', '740.00']
['', '', '', '']
['Shipping labels, roll of 500', '25', '12.00', '300.00']
['', '', '', '']
['Packing tape, 48 mm, pack of 6', '30', '7.20', '216.00']
['', '', '', '']
['Delivery', '1', '44.00', '44.00']

The data rows are right. Two things need cleaning: the empty rows, which come from the space between lines, and the header, where "Quantity" and "Unit price" sit so close that the boundary landed in the wrong place. Drop the empty rows, and write the header yourself when you know the columns:

Python
rows = [row for row in table[1:] if any(row)]

PyMuPDF: find_tables

PyMuPDF has a table finder with the same two approaches. The text strategy handled this page without a crop:

Python
import pymupdf

with pymupdf.open("invoice.pdf") as doc:
    table = doc[0].find_tables(strategy="text").tables[0]
    for row in table.extract()[:6]:
        print(row)
Output
['Description', 'Quantity', 'Unit price', 'Amount']
['A4 copy paper, 80 gsm, box of 5 reams', '40', '18.50', '740.00']
['Shipping labels, roll of 500', '25', '12.00', '300.00']
['Packing tape, 48 mm, pack of 6', '30', '7.20', '216.00']
['Delivery', '1', '44.00', '44.00']
['', 'Subtotal', '', '1,300.00']

The header is right here, and there are no empty rows. It also took in the totals below the table as extra rows, which you may or may not want; stop at the first row whose first cell is empty.

The low-tech way: layout text and a split

When a table's columns are clearly apart, you do not need a table finder at all. pdftotext -layout keeps the columns in place with spaces, and two or more spaces mark a column break:

Python
import re
import subprocess

text = subprocess.run(["pdftotext", "-layout", "invoice.pdf", "-"], capture_output=True, text=True).stdout
for line in text.split("\n"):
    if re.search(r"\d\.\d{2}$", line.strip()):
        print(re.split(r"\s{2,}", line.strip()))
Output
['A4 copy paper, 80 gsm, box of 5 reams', '40', '18.50', '740.00']
['Shipping labels, roll of 500', '25', '12.00', '300.00']
['Packing tape, 48 mm, pack of 6', '30', '7.20', '216.00']
['Delivery', '1', '44.00', '44.00']
['Subtotal', '1,300.00']
['VAT 18%', '234.00']
['Total due', '1,534.00']

The filter keeps only lines that end in an amount. This breaks when a cell is empty, because the split cannot tell which column is missing, and when a description contains a wide gap of its own.

Which to use

SituationUse
Every cell has borderspdfplumber or PyMuPDF with their defaults
No borders, clear gapsPyMuPDF find_tables(strategy="text"), or the layout split
You know where the table ispdfplumber with a crop
Cells that wrap over several linesExpect manual clean-up with any tool
A scanned tableOCR first; positions are then approximate, and results are rougher

Check what you extracted

A table that comes out with the wrong number of columns in one row will poison everything after it. Three cheap checks:

  • every row has the same number of cells;
  • numeric columns contain only numbers;
  • if the document prints a total, the column adds up to it.

The invoice guide does the third check on this file.

Tables and 100sands

100sands handles tables in two ways, both for PDFs and HTML files that contain text, not scans.

/v1/text returns each row of a table as one line, with the cells separated by tabs. For the invoice used here, the table's lines in the response's text have this shape, with a tab between cells:

Output
Description	Quantity	Unit price	Amount
A4 copy paper, 80 gsm, box of 5 reams	40	18.50	740.00
Shipping labels, roll of 500	25	12.00	300.00

Splitting each line at the tabs gives you the cells, with the same check as above: every row should have the same number of them. It is not a table finder, though: it does not tell you where a table starts or ends, and a cell that wraps onto a second line becomes a line of its own.

For documents of one kind, such as invoices from one supplier, a document type can have a table field. It returns the table as rows with named columns, with numbers as numbers, from one call. The PDF data extraction API page shows it. For a one-off table in a text PDF, the libraries above are the shorter path.

Questions

Why does pdfplumber extract_tables() return an empty list?

Its default looks for cells enclosed by drawn lines. A table whose columns are separated only by space has none. Pass {"vertical_strategy": "text", "horizontal_strategy": "text"} and crop the page to the table first.

What is the best Python library for extracting tables from PDFs?

pdfplumber and PyMuPDF both find tables. On our sample, a table without column lines, PyMuPDF's find_tables with the text strategy gave clean rows with the right header and no cropping.

How do I extract a table from a scanned PDF?

Run OCR first to get words with positions, then group them into rows and columns. Positions from OCR are approximate, so expect to clean the result by hand more often than with a text PDF.

Try it on your own documents

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

Start free