Without ruling lines, pdfplumber and camelot find 0 tables at their defaults. Here is the fix for each in Python, or send the file to the API for $0.02 a page, 50 pages free: 2 pages cost $0.04.
Extract tables from a PDF in Python
extract_tables() and camelot's lattice flavor look for ruling lines, so rows on shaded bands give 0 tables. Below is what each tool returns on a 2-page price list, on merged cells and on a scan, and the API call for the digital file.
GuidesUpdated
Step by step
Extract tables from a PDF in Python
Look for ruling lines with pdfplumber
Start with pdfplumber on table-larkspur.pdf, a made-up nursery price list: 2 pages, 52 rows, 6 columns and no ruling lines.
extract_tables()returns 0 tables on both pages, because its defaultlinesstrategy uses the page's graphical lines as cell borders. Withvertical_strategyandhorizontal_strategyset totextyou get 61 rows by 8 columns on page 1 and 51 by 7 on page 2. Words split across cells (Wholeandsale price), blank rows appear, and the 2 pages don't agree on a column count.tables_pdfplumber.py import pdfplumber TEXT = {"vertical_strategy": "text", "horizontal_strategy": "text"} with pdfplumber.open("table-larkspur.pdf") as pdf: for page in pdf.pages: ruled = page.extract_tables() rows = page.extract_tables(TEXT)[0] print(f"page {page.page_number}: {len(ruled)} tables from lines, text strategy {len(rows)} rows x {len(rows[0])} columns") if page.page_number == 1: first_rows = rows[:5] for row in first_rows: print(row) # $ python tables_pdfplumber.py # page 1: 0 tables from lines, text strategy 61 rows x 8 columns # page 2: 0 tables from lines, text strategy 51 rows x 7 columns # ['Whole', 'sale price', 'list, spring 2', '027', '', '', '', ''] # ['', '', '', '', '', '', '', ''] # ['rices in EUR', 'per plant, excluding V', 'AT. Minimum order per line i', 'n plant', 's.', '', '', ''] # ['', '', '', '', '', '', '', ''] # ['CODE', 'PLANT', '', 'POT', 'HEIGHT', 'PRICE', '(EUR) MIN.', 'ORDER']Try camelot's four flavors
camelot 2.0.0 has four flavors.
latticereturns 0 tables: it parses tables with demarcated lines between cells.streamfinds 3, two overlapping 31 by 5 tables on page 1, each missing one of the 6 columns, and 26 by 6 on page 2.networkandhybridagree here. Page 1 comes back 29 by 6, the header plus all 28 rows. Page 2 is 26 by 6, and its first row holds the repeated page title and nursery name, so you drop that row and join the 2 pages yourself.tables_camelot.py import camelot for flavor in ["lattice", "stream", "network", "hybrid"]: tables = camelot.read_pdf("table-larkspur.pdf", pages="1-2", flavor=flavor) print(flavor, [(t.page, t.shape, round(t.parsing_report["accuracy"], 2)) for t in tables]) page_two = camelot.read_pdf("table-larkspur.pdf", pages="2", flavor="network")[0].df print(page_two.iloc[0].tolist()) # $ python tables_camelot.py # lattice [] # stream [(1, (31, 5), 98.85), (1, (31, 5), 99.77), (2, (26, 6), 99.34)] # network [(1, (29, 6), 100.0), (2, (26, 6), 99.29)] # hybrid [(1, (29, 6), 100.0), (2, (26, 6), 99.29)] # ['Wholesale price list, spring 2027', '', '', '', '', 'Larkspur Hollow Nursery']Fill merged cells
Ruled tables bring a different problem. In larkspur-trade.pdf, a ruled table we made for this guide,
Treesspans 3 rows of the GROUP column and one header spans 3 price columns. pdfplumber putsNonein spanned cells and camelot'slatticegives empty strings. Withcopy_text=["v", "h"], camelot writesTreesinto every row it spans, but it also copies GROUP and PLANT down into the second header row, so the header still needs cleaning by hand.tables_merged.py import camelot import pdfplumber with pdfplumber.open("larkspur-trade.pdf") as pdf: rows = pdf.pages[0].extract_tables()[0] print("pdfplumber", rows[1:4]) plain = camelot.read_pdf("larkspur-trade.pdf", flavor="lattice")[0].df filled = camelot.read_pdf("larkspur-trade.pdf", flavor="lattice", copy_text=["v", "h"])[0].df print("lattice", plain.iloc[1:4, :3].values.tolist()) print("copy_text", filled.iloc[1:4, :3].values.tolist()) # $ python tables_merged.py # pdfplumber [[None, None, '1-9', '10-49', '50+'], ['Trees', "Acer palmatum 'Bloodgood'", '24.50', '22.80', '21.10'], [None, "Betula utilis 'Doorenbos'", '58.00', '54.00', '50.50']] # lattice [['', '', '1-9'], ['Trees', "Acer palmatum 'Bloodgood'", '24.50'], ['', "Betula utilis 'Doorenbos'", '58.00']] # copy_text [['GROUP', 'PLANT', '1-9'], ['Trees', "Acer palmatum 'Bloodgood'", '24.50'], ['Trees', "Betula utilis 'Doorenbos'", '58.00']]Check for a text layer first
Scans fail before any table logic runs. We rendered page 1 as a slightly crooked, speckled image and saved it as a PDF. pdfplumber finds 0 characters, and camelot returns 0 tables with a warning that the page is image-based. Tesseract 5.5.0 on a 300 dpi render reads the rows but garbles cells:
3,15"for 3.15, a euro sign for the C in C25 and a cent sign for the C in C3.tables_scan.py import camelot import pdfplumber import pytesseract print(camelot.read_pdf("larkspur-scan.pdf", flavor="stream").n, "tables from camelot") with pdfplumber.open("larkspur-scan.pdf") as pdf: page = pdf.pages[0] print(len(page.chars), "characters in the text layer") image = page.to_image(resolution=300).original lines = [line for line in pytesseract.image_to_string(image).splitlines() if "LH" in line] for line in lines[:8]: print(ascii(line)) # $ python tables_scan.py # .../camelot/parsers/base.py:132: UserWarning: page-1 is image-based, camelot only works on text-based pages. # warnings.warn( # 0 tables from camelot # 0 characters in the text layer # 'LH-1001 Acer palmatum \u2018Bloodgood c10. 80-100 cm 24.50 | 5' # "LH-1002 Acer palmatum 'Sango-kaku' - C10 80-100 cm \u2014 26.80 sy" # '. LH-1003 i Amelanchier jamarcelle | 15 125-150 cm. 31.20, 3' # "\u2018LH-1004 Betula utilis \u2018Doorenbos' ~ \u20ac25 200-250 cm : 58.00 2" # '. LH-1005 Buxus sempervirens: C2 20-25 cm 3,15" 25' # 'LH-1006 Carpinus betulus | \xa23 60-80 cm 4.90. 25' # 'LH-1007 Cornus alba \u2018Sibirica\u2019 C3. 40-60 cm 4.40 10\xb0' # '. LH-1009 Euonymus alatus \u2018Compactus\u2019 . C5: 40-50 cm 9.80 10'OCR the scan, then run camelot
camelot's docs recommend adding a text layer first and point to OCRmyPDF. After
ocrmypdf --deskewwe compared every row with the digital file, ignoring spaces and punctuation.streamfound all 28 rows but read only 10 right.networkfound 25 and read 9 right.hybridsplit the page into 6 tables, found all 28 rows and read 10 right. Wrong cells holdS15:for 3.15,cS)for C3, a stray dot inC10., and the code and plant name of LH-1006 in one cell. Nothing in camelot's output marks which rows are wrong.tables_scan_ocr.py import re import subprocess import camelot subprocess.run(["ocrmypdf", "--deskew", "--quiet", "larkspur-scan.pdf", "larkspur-scan-ocr.pdf"], check=True) def rows_by_code(table): rows = {} for row in table.df.values.tolist(): text = re.sub(r"[^\w.-]", "", "".join(row)) if text.startswith("LH-"): rows[text[:7]] = (text, [" ".join(cell.split()) for cell in row]) return rows digital = {code: text for code, (text, _) in rows_by_code(camelot.read_pdf("table-larkspur.pdf", flavor="network")[0]).items()} for flavor in ["stream", "network", "hybrid"]: tables = camelot.read_pdf("larkspur-scan-ocr.pdf", flavor=flavor) found = {} for table in tables: for code, row in rows_by_code(table).items(): if code not in found or row[0] == digital.get(code): found[code] = row right = [code for code, (text, _) in found.items() if text == digital.get(code)] print(f"{flavor}: {tables.n} tables, {len(found)} of 28 rows found, {len(right)} read right") wrong = [cells for code, (text, cells) in sorted(found.items()) if text != digital.get(code)] for cells in wrong[:3]: print(ascii(cells)) # $ python tables_scan_ocr.py # stream: 1 tables, 28 of 28 rows found, 10 read right # network: 1 tables, 25 of 28 rows found, 9 read right # hybrid: 6 tables, 28 of 28 rows found, 10 read right # ['LH-1001', 'Acer palmatum \u2018Bloodgood\u2019', 'C10.', '80-100 cm', '24.50', '5'] # ['LH-1005', 'Buxus sempervirens', 'c2', '20-25 cm', 'S15:', '25'] # ['LH-1006 = Carpinus betulus', '', 'cS)', '60-80 cm', '4.90', '25']Send the same file to the API
Skip the cleanup code: post the file to parserkit with the
generic_tablepreset. The API example below runs the digital file. One call returns 1 table across both pages with 52 rows and 6 columns, no repeated title row, and PRICE (EUR) and MIN. ORDER come back as numbers. Each cell is matched against the words printed inside its own box, and scanned pages go through the same call, read with OCR, so there is no OCRmyPDF step to bolt on. The 2 pages cost $0.04, the first 50 pages are free, and volume rates are on pricing.
Example
table-larkspur.pdf through the generic_table preset
The recorded run of the digital price list: the request, and the response as it came back.
POSTapi.parserkit.com/v1/parse?wait=60
curl "https://api.parserkit.com/v1/parse?wait=60" \
-H "Authorization: Bearer $PARSERKIT_API_KEY" \
-F file=@table-larkspur.pdf \
-F preset=generic_tableimport os
import requests
with open("table-larkspur.pdf", "rb") as file:
res = requests.post(
"https://api.parserkit.com/v1/parse",
params={"wait": 60},
headers={"Authorization": f"Bearer {os.environ['PARSERKIT_API_KEY']}"},
files={"file": file},
data={"preset": "generic_table"},
)
job = res.json()
print(job["verification"]["status"], job["verification"]["coverage"])import { openAsBlob } from "node:fs";
const form = new FormData();
form.append("file", await openAsBlob("table-larkspur.pdf"), "table-larkspur.pdf");
form.append("preset", "generic_table");
const res = await fetch("https://api.parserkit.com/v1/parse?wait=60", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.PARSERKIT_API_KEY}` },
body: form,
});
const job = await res.json();
console.log(job.verification.status, job.verification.coverage);{
"id": "job_2tikgoo3g3k5gvq33ox9",
"status": "succeeded",
"preset": "generic_table",
"pages": 2,
"cost_usd": 0.04,
"verification_status": "verified",
"data": {
"tables": [
{
"title": "Prices in EUR per plant, excluding VAT. Minimum order per line in plants.",
"page_start": 1,
"page_end": 2,
"columns": [
{
"name": "CODE",
"type": "string"
},
{
"name": "PLANT",
"type": "string"
},
{
"name": "POT",
"type": "string"
}
],
"rows": [
[
"LH-1001",
"Acer palmatum 'Bloodgood'",
"C10",
"80-100 cm",
24.5,
5
],
[
"LH-1002",
"Acer palmatum 'Sango-kaku'",
"C10",
"80-100 cm",
26.8,
5
],
[
"LH-1003",
"Amelanchier lamarckii",
"C15",
"125-150 cm",
31.2,
3
],
[
"LH-1004",
"Betula utilis 'Doorenbos'",
"C25",
"200-250 cm",
58,
2
],
[
"LH-1005",
"Buxus sempervirens",
"C2",
"20-25 cm",
3.15,
25
],
[
"LH-1006",
"Carpinus betulus",
"C3",
"60-80 cm",
4.9,
25
],
[
"LH-1007",
"Cornus alba 'Sibirica'",
"C3",
"40-60 cm",
4.4,
10
],
[
"LH-1008",
"Cornus kousa 'China Girl'",
"C15",
"100-125 cm",
42.6,
3
],
[
"LH-1009",
"Euonymus alatus 'Compactus'",
"C5",
"40-50 cm",
9.8,
10
],
[
"LH-1010",
"Fagus sylvatica",
"C3",
"60-80 cm",
5.2,
25
],
[
"LH-1011",
"Hamamelis x intermedia 'Arnold Promise'",
"C10",
"60-80 cm",
36.5,
3
],
[
"LH-1012",
"Hydrangea paniculata 'Limelight'",
"C5",
"40-60 cm",
11.9,
10
],
[
"LH-1013",
"Hydrangea macrophylla 'Endless Summer'",
"C5",
"30-40 cm",
12.4,
10
],
[
"LH-1014",
"Ilex crenata 'Dark Green'",
"C3",
"25-30 cm",
5.6,
25
],
[
"LH-1015",
"Lavandula angustifolia 'Hidcote'",
"P9",
"10-15 cm",
1.95,
50
],
[
"LH-1016",
"Ligustrum ovalifolium",
"C3",
"60-80 cm",
3.8,
25
],
[
"LH-1017",
"Magnolia stellata",
"C10",
"60-80 cm",
34.9,
3
],
[
"LH-1018",
"Malus 'Evereste'",
"C25",
"200-250 cm",
64,
2
],
[
"LH-1019",
"Nandina domestica",
"C5",
"40-50 cm",
13.2,
10
],
[
"LH-1020",
"Osmanthus x burkwoodii",
"C5",
"40-50 cm",
14.6,
10
],
[
"LH-1021",
"Photinia x fraseri 'Red Robin'",
"C5",
"60-80 cm",
10.8,
10
],
[
"LH-1022",
"Pieris japonica 'Mountain Fire'",
"C5",
"30-40 cm",
13.9,
10
],
[
"LH-1023",
"Prunus laurocerasus 'Rotundifolia'",
"C5",
"60-80 cm",
9.4,
10
],
[
"LH-1024",
"Prunus serrulata 'Kanzan'",
"C25",
"200-250 cm",
61.5,
2
],
[
"LH-1025",
"Pyrus calleryana 'Chanticleer'",
"C25",
"250-300 cm",
72,
2
],
[
"LH-1026",
"Rhododendron 'Cunningham's White'",
"C10",
"40-50 cm",
22.8,
5
],
[
"LH-1027",
"Rosa 'Gertrude Jekyll'",
"C4",
"40-60 cm",
12.9,
10
],
[
"LH-1028",
"Rosa rugosa 'Alba'",
"C3",
"40-60 cm",
6.4,
25
],
[
"LH-1029",
"Salvia nemorosa 'Caradonna'",
"P9",
"10-15 cm",
2.25,
50
],
[
"LH-1030",
"Sambucus nigra 'Black Lace'",
"C5",
"60-80 cm",
12.7,
10
],
[
"LH-1031",
"Skimmia japonica 'Rubella'",
"C3",
"20-25 cm",
8.6,
10
],
[
"LH-1032",
"Sorbus aucuparia",
"C15",
"150-175 cm",
33.4,
3
],
[
"LH-1033",
"Spiraea japonica 'Goldflame'",
"C3",
"30-40 cm",
5.1,
25
],
[
"LH-1034",
"Syringa vulgaris 'Madame Lemoine'",
"C10",
"80-100 cm",
27.9,
5
],
[
"LH-1035",
"Taxus baccata",
"C5",
"40-50 cm",
11.2,
10
],
[
"LH-1036",
"Thuja occidentalis 'Smaragd'",
"C10",
"100-125 cm",
17.8,
10
],
[
"LH-1037",
"Viburnum opulus 'Roseum'",
"C5",
"60-80 cm",
11.6,
10
],
[
"LH-1038",
"Viburnum tinus",
"C5",
"40-60 cm",
10.9,
10
],
[
"LH-1039",
"Weigela florida 'Bristol Ruby'",
"C3",
"40-60 cm",
5.7,
25
],
[
"LH-1040",
"Anemone x hybrida 'Honorine Jobert'",
"P9",
"10-15 cm",
2.45,
50
],
[
"LH-1041",
"Astilbe chinensis 'Pumila'",
"P9",
"10-15 cm",
2.1,
50
],
[
"LH-1042",
"Echinacea purpurea 'Magnus'",
"P9",
"10-15 cm",
2.35,
50
],
[
"LH-1043",
"Geranium 'Rozanne'",
"P9",
"10-15 cm",
2.9,
50
],
[
"LH-1044",
"Hosta 'Patriot'",
"C2",
"15-20 cm",
4.8,
25
],
[
"LH-1045",
"Nepeta x faassenii",
"P9",
"10-15 cm",
2.05,
50
],
[
"LH-1046",
"Pennisetum alopecuroides 'Hameln'",
"C2",
"30-40 cm",
4.6,
25
],
[
"LH-1047",
"Rudbeckia fulgida 'Goldsturm'",
"P9",
"10-15 cm",
2.15,
50
],
[
"LH-1048",
"Sedum 'Herbstfreude'",
"P9",
"10-15 cm",
1.95,
50
],
[
"LH-1049",
"Stipa tenuissima",
"P9",
"20-30 cm",
2.2,
50
],
[
"LH-1050",
"Verbena bonariensis",
"P9",
"10-15 cm",
1.9,
50
],
[
"LH-1051",
"Calamagrostis 'Karl Foerster'",
"C2",
"40-60 cm",
4.7,
25
],
[
"LH-1052",
"Miscanthus sinensis 'Gracillimus'",
"C3",
"60-80 cm",
7.4,
10
]
],
"row_pages": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]
}
]
},
"verification": {
"status": "verified",
"coverage": {
"values": 319,
"checked": 319
}
},
"outputs": [
{
"name": "output.json",
"bytes": 75337
},
{
"name": "output.xlsx",
"bytes": 10061
},
{
"name": "output.csv",
"bytes": 3109
}
],
"warnings": []
}Real response from . Trimmed to 3 of 6 columns, with bounding boxes, empty fields and file links left out.
Try it on this fileWhat each tool returned on the same price list
table-larkspur.pdf: 2 pages, 52 rows, 6 columns, no ruling lines. Shapes are rows x columns.
| Tool and setting | What came back | Left for you to fix |
|---|---|---|
| parserkit generic_table | 1 table over both pages, 52 x 6, price and minimum order as numbers; each cell found among the words printed inside its own box | Nothing to join |
| pdfplumber extract_tables(), default | 0 tables on either page | Switch to the text strategy or another tool |
| pdfplumber, text strategy | 61 x 8 on page 1 and 51 x 7 on page 2, words cut across cells | Rebuild the 6 columns, drop blank and title rows |
| camelot lattice | 0 tables | Needs ruling lines between cells |
| camelot stream | 2 overlapping 31 x 5 tables on page 1, 26 x 6 on page 2 | Merge page 1, drop the title row, join the pages |
| camelot network or hybrid | 29 x 6 on page 1, 26 x 6 on page 2 | Drop the repeated title row, join the 2 pages |
Where the scripts break
Every fix above is tuned to one file. The text strategy gave 8 columns on page 1 and 7 on page 2, copy_text filled the merged cells but left a header to clean by hand, and after OCR camelot read 10 of 28 rows right with nothing marking the other 18. A new supplier's layout means new settings, and a wrong cell goes straight into your sheet.
parserkit's generic_table preset takes each file as it comes, digital or scanned, and returns one table per table, kept whole across page breaks, as JSON, an Excel sheet and a CSV.
What we ran
Every script here ran on Python 3.12.14 in a virtual environment made for this guide, on the files named in each step. The lines under # $ in each code block are what the script printed, cut only where you see ....
table-larkspur.pdf is one of parserkit's public samples. larkspur-trade.pdf and the scan were made for this guide; the scripts and outputs, including the one that drew the merged-cell table, are linked under Sources.
import platform
import subprocess
import sys
from importlib.metadata import version
TOOLS = {
"tesseract": lambda: subprocess.run(["tesseract", "--version"], capture_output=True, text=True).stdout.split()[1],
"ollama": lambda: subprocess.run(["ollama", "--version"], capture_output=True, text=True).stdout.split()[-1],
"libreoffice": lambda: subprocess.run(["soffice", "--version"], capture_output=True, text=True).stdout.split()[1],
}
print("Python", platform.python_version())
for name in sys.argv[1:]:
print(name, TOOLS[name]() if name in TOOLS else version(name))
# $ python versions.py pdfplumber camelot-py pytesseract ocrmypdf tesseract
# Python 3.12.14
# pdfplumber 0.11.10
# camelot-py 2.0.0
# pytesseract 0.3.13
# ocrmypdf 17.13.0
# tesseract 5.5.0FAQ
Common questions
Why does pdfplumber's extract_tables() return an empty list?
By default extract_tables() uses the lines strategy, which takes the page's graphical lines as the borders of table cells. A table drawn with whitespace or shaded bands has no such lines, so you get 0 tables. Set both strategies to text, or try camelot's network flavor. On a scan with no text layer, pdfplumber finds 0 characters as well. parserkit's generic_table preset needs neither ruling lines nor a text layer, and it returned this price list as one 52-row table.
Can camelot read a scanned PDF?
Not by itself. Its FAQ says: "Camelot only works with text-based PDFs and not scanned documents." On our scan it warned that the page is image-based and returned 0 tables. The docs recommend adding a text layer with OCRmyPDF first. There is also an opt-in ml flavor that needs PyTorch and an OCR extra for scans; we did not test it. parserkit reads scanned pages with OCR in the same call as digital ones.
Which camelot flavor should I use?
Use lattice when the table has drawn lines between cells. Without lines, try network or hybrid before stream. On our borderless price list, stream split page 1 into two 5-column tables, while network and hybrid got all 6 columns and all 28 rows of page 1. Or skip the choice: parserkit returned both pages as one 6-column table in one call.
How do I join a table that runs over two pages?
With camelot you drop the repeated title row from page 2 and concatenate the 2 DataFrames, once their columns agree. parserkit returns a table that runs over a page break as one table, with row_pages giving the page of every row.
What does the table API cost?
$0.02 a page, and $0.01 a page past 5,000 pages in a month. Signing in with Google gives you $1.00 of credit, which is 50 pages free. Failed jobs cost nothing. The 2-page price list cost $0.04.
Related
- PDF table extraction APITables from PDFs and scans as JSON, Excel and CSV, whole across page breaks.
- PDF to Excel converterPull the tables out of a PDF, digital or scanned, into Excel with the printed column names, free and without an account.
- PDF to JSON converterTurn the tables in a PDF into JSON rows with typed columns and the page of every row, free without an account.
- parserkit vs TabulaTabula's free draw-a-box table extraction for text-based PDFs against parserkit's OCR, page-spanning tables and API at $0.02 a page.
- PDF table extraction toolsparserkit leads for PDF tables to Excel, CSV and JSON at $0.02 a page, ahead of Tabula, Camelot and AWS Textract.
- Extract invoice data with PythonText layer or Tesseract OCR, then a local LLM fills a JSON schema, then Python checks every total before it reaches your books.
- Parse bank statements in Pythonpdfplumber by word position on a 3-page statement: rows that cross a page, DR and CR amounts, and a running-balance check in code.
- camelot read_pdf finds no tablesWhy Camelot returns TableList n=0 on scans and borderless tables, the fix for each, and how parserkit reads the scan in one call.
- tabula read_pdf returns []tabula-py gives an empty list on scans and JavaNotFoundError without Java. Both reproduced, the fix for each, and parserkit reading the scan.
- extract_text returns emptypdfplumber and pypdf return '' on scanned PDFs. Spot a scan in one line, then let parserkit read it with OCR and checked totals.
- What is the best PDF library?parserkit leads for fields and tables out of PDFs at $0.02 a page, ahead of pypdf, pdfplumber and PyMuPDF.
Start free
Send us your worst scan
Your first $1 is free: 50 pages with every check and export, no card.
