Table Extraction from Documents
Extract tables from PDF documents with support for merged cells and multi-page layouts using camelot.
Financial reports, research papers, and government filings have tables locked inside PDFs that you need in a spreadsheet — but copy-paste destroys the formatting. This playbook extracts tables from PDFs using Camelot with support for merged cells, multi-page layouts, and complex table structures.
Who it's for: data analysts extracting tabular data from PDF financial reports and regulatory filings, researchers pulling data tables from published scientific papers for meta-analysis, finance teams converting PDF bank statements and invoices into spreadsheet format, operations managers extracting pricing and inventory tables from vendor PDF catalogs, compliance teams digitizing tabular data from scanned audit documents
Example
"Extract all tables from this 50-page annual report PDF" → Table extraction pipeline: PDF page analysis detecting table boundaries and structure, Camelot extraction handling merged cells and multi-column layouts, per-table quality validation checking row/column alignment, output as clean pandas DataFrames or CSV files, and batch processing for multi-page tables spanning across page breaks
CLAUDE.md Template
New here? 3-minute setup guide → | Already set up? Copy the template below.
npx ccplaybooks add table-extractor# Table Extractor
## Overview
This workflow enables precise extraction of tables from PDF documents using **camelot** - the gold standard for PDF table extraction. Handle complex tables with merged cells, borderless tables, and multi-page layouts with high accuracy.
## How to Use
1. Provide the PDF containing tables
2. Optionally specify pages or table detection method
3. I'll extract tables as pandas DataFrames
Get new playbooks like this one
One email a week with new Claude Code workflows. Free, like everything here.
No spam. Unsubscribe anytime.
What This Does
This workflow enables precise extraction of tables from PDF documents using camelot - the gold standard for PDF table extraction. Handle complex tables with merged cells, borderless tables, and multi-page layouts with high accuracy.
Quick Start
Step 1: Create a Project Folder
mkdir -p ~/Documents/TableExtractor
Step 2: Download the Template
Click Download above, then:
mv ~/Downloads/CLAUDE.md ~/Documents/TableExtractor/
Step 3: Start Working
cd ~/Documents/TableExtractor
claude
How to Use
- Provide the PDF containing tables
- Optionally specify pages or table detection method
- I'll extract tables as pandas DataFrames
Example prompts:
- "Extract all tables from this PDF"
- "Get the table on page 5 of this report"
- "Extract borderless tables from this document"
- "Convert PDF tables to Excel format"
Best Practices
- Try Both Methods: Lattice for bordered, stream for borderless
- Check Accuracy Score: Above 90% is usually good
- Use Visual Debugging: Understand extraction results
- Specify Areas: For PDFs with multiple table types
- Handle Headers: First row often needs special treatment
Examples
Example 1: Financial Statement Extraction
import camelot
import pandas as pd
def extract_financial_tables(pdf_path):
"""Extract financial tables from annual report."""
# Extract all tables
tables = camelot.read_pdf(pdf_path, pages='all', flavor='lattice')
financial_data = {
'income_statement': None,
'balance_sheet': None,
'cash_flow': None,
'other_tables': []
}
for table in tables:
df = table.df
text = df.to_string().lower()
# Identify table type
if 'revenue' in text or 'sales' in text:
if 'operating income' in text or 'net income' in text:
financial_data['income_statement'] = df
elif 'asset' in text and 'liabilities' in text:
financial_data['balance_sheet'] = df
elif 'cash flow' in text or 'operating activities' in text:
financial_data['cash_flow'] = df
else:
financial_data['other_tables'].append({
'page': table.page,
'data': df,
'accuracy': table.accuracy
})
return financial_data
financials = extract_financial_tables('annual_report.pdf')
if financials['income_statement'] is not None:
print("Income Statement found:")
print(financials['income_statement'])
Example 2: Scientific Data Extraction
import camelot
import pandas as pd
def extract_research_data(pdf_path, pages='all'):
"""Extract data tables from research paper."""
# Try lattice for bordered tables
tables = camelot.read_pdf(pdf_path, pages=pages, flavor='lattice')
if not tables or all(t.accuracy < 70 for t in tables):
# Fall back to stream for borderless
tables = camelot.read_pdf(pdf_path, pages=pages, flavor='stream')
extracted_data = []
for table in tables:
df = table.df
# Clean up the DataFrame
# Set first row as header if it looks like one
if not df.iloc[0].str.contains(r'\d').any():
df.columns = df.iloc[0]
df = df[1:]
df = df.reset_index(drop=True)
extracted_data.append({
'page': table.page,
'accuracy': table.accuracy,
'data': df
})
return extracted_data
data = extract_research_data('research_paper.pdf')
for i, item in enumerate(data):
print(f"Table {i+1} (Page {item['page']}, Accuracy: {item['accuracy']}%):")
print(item['data'].head())
Example 3: Invoice Line Items
Limitations
- Encrypted PDFs not supported
- Image-based PDFs need OCR preprocessing
- Very complex merged cells may need tuning
- Rotated tables require preprocessing
- Large PDFs may need page-by-page processing
Installation
pip install camelot-py[cv]
# Additional dependencies
# macOS
brew install ghostscript tcl-tk
# Ubuntu
apt-get install ghostscript python3-tk