Python PDF Table Extraction: Complete Tutorial
Automate PDF table extraction with Python. This tutorial covers three popular libraries with working code examples.
Setup
pip install camelot-py[cv] tabula-py pdfplumber pandas openpyxl
Note: Camelot requires Ghostscript. Tabula requires Java.
Method 1: Camelot
Best for tables with clear borders (lattice) or consistent spacing (stream).
import camelot
# For bordered tables
tables = camelot.read_pdf('document.pdf', pages='1', flavor='lattice')
# For borderless tables
tables = camelot.read_pdf('document.pdf', pages='1', flavor='stream')
# Export to Excel
tables[0].to_excel('output.xlsx')
# Get as pandas DataFrame
df = tables[0].df
Method 2: Tabula-py
Java-based engine, good general-purpose extraction.
import tabula
# Extract all tables from all pages
tables = tabula.read_pdf('document.pdf', pages='all')
# Returns list of DataFrames
for i, table in enumerate(tables):
table.to_excel(f'table_{i}.xlsx')
# Specify area (top, left, height, width in points)
tables = tabula.read_pdf('document.pdf',
area=[0, 0, 500, 800],
pages='1')
Method 3: pdfplumber
Pure Python, no external dependencies. Good for simple tables.
import pdfplumber
import pandas as pd
with pdfplumber.open('document.pdf') as pdf:
page = pdf.pages[0]
table = page.extract_table()
# Convert to DataFrame
df = pd.DataFrame(table[1:], columns=table[0])
df.to_excel('output.xlsx', index=False)
Which Library to Choose?
- Camelot: Best accuracy, especially for bordered tables
- Tabula-py: Good balance of features and ease of use
- pdfplumber: Simplest setup, no Java/Ghostscript needed
Handling Edge Cases
# Multiple pages
tables = camelot.read_pdf('doc.pdf', pages='1-5,8')
# Scanned PDFs (need OCR first)
# Use pytesseract or cloud OCR, then extract from text layer
# Tables spanning pages
# Extract separately, then concatenate DataFrames
df_combined = pd.concat([t.df for t in tables])
Don't Want to Code?
Extract PDF tables instantly in your browser — no Python required.
Try TablePDF Free →