PDF to Excel in Python: Convert Tables to Multi-Sheet XLSX
Direct answer: Extract the PDF’s pages with pdfmux — extract_json("report.pdf")["pages"] gives you each page’s Markdown text — then parse the Markdown pipe-tables out of each page into {"headers": [...], "rows": [[...]], "page": N} dicts (pdfmux has no structured table object; tables render as Markdown) and write each one to its own sheet in an .xlsx file with pandas and openpyxl. This is where Excel differs from CSV: a CSV is one flat table per file, so a PDF with three distinct tables becomes three loose files with no types and no structure. An .xlsx workbook holds all three tables as separate sheets in one file, preserves numeric and date types, and keeps formatting. Reach for Excel when a PDF has several distinct tables you want to keep together; reach for CSV when you have one table feeding a script or database load.
Why PDF-to-Excel is harder than it looks
A PDF has no concept of a table. There are no rows, no columns, no cells — only glyphs painted at (x, y) coordinates and, sometimes, line segments drawn nearby. “Extracting a table” means reconstructing structure that was thrown away when the document was rendered. Four problems show up constantly:
- No table structure. The extractor has to infer cell boundaries from line geometry or whitespace alignment. Two visually identical tables can need completely different detection logic depending on whether the borders are real lines or just gaps.
- Multiple tables per page. Financial statements and reports routinely stack a summary table above a detail table on the same page. A naive extractor merges them into one ragged grid. For Excel, you want them as separate sheets, which means the extractor has to segment them first.
- Merged and spanning cells. A header that spans three columns, or a label that spans two rows, breaks the rectangular grid assumption. The extractor has to decide how to fill the gaps.
- Numbers stored as text. Everything extracted from a PDF arrives as a string.
"1,234.50"and"(420)"are text, not a float and a negative number. Drop them into Excel as-is and every numeric column is left-aligned text you can’t sum, sort, or chart.
pdfmux handles the first problem at extraction time — it classifies each page and routes it to the best extractor (PyMuPDF for clean digital pages, Docling for complex tables) — but it has no structured {headers, rows} table object in its public API. What comes back is Markdown, with each table rendered as a pipe-table; if a page has several distinct tables, you get several distinct pipe-tables in that page’s text, and you segment them yourself with a small parser. The fourth problem (number typing) is a post-processing step you control, covered in Step 3.
Step 1: Extract pages and parse their tables
Install pdfmux and run extract_json, which gives you real per-page text (result["pages"]) to parse pipe-tables out of and tag with a page number.
pip install pdfmux pandas openpyxl
import re
from pdfmux import extract_json
def markdown_tables(markdown_text: str) -> list[dict]:
"""Parse every Markdown pipe-table into {"headers": [...], "rows": [[...]]}."""
tables, lines, i = [], markdown_text.splitlines(), 0
while i < len(lines):
if lines[i].strip().startswith("|") and i + 1 < len(lines) and re.match(
r"^\|[\s:|-]+\|$", lines[i + 1].strip()
):
headers = [c.strip() for c in lines[i].strip().strip("|").split("|")]
i += 2
rows = []
while i < len(lines) and lines[i].strip().startswith("|"):
rows.append([c.strip() for c in lines[i].strip().strip("|").split("|")])
i += 1
tables.append({"headers": headers, "rows": rows})
else:
i += 1
return tables
result = extract_json("report.pdf", quality="standard")
tables = []
for page in result["pages"]:
for t in markdown_tables(page["text"]):
t["page"] = page["page"]
tables.append(t)
for table in tables:
print(f"Page {table['page']}: "
f"{len(table['headers'])} columns, {len(table['rows'])} rows")
Each entry in tables is a dictionary with three keys:
headers— the column names as a list of strings.rows— a list of rows, each row a list of cell strings.page— the source page number, so you can label sheets or trace a value back to its origin.
The quality="standard" setting runs the full extract-audit-repair pipeline: it extracts, audits every page against quality checks, and re-extracts anything that fails. That matters here because a half-detected table produces a half-correct spreadsheet, and a wrong number in a finance workbook is worse than a missing one. For the mechanics of how detection works, see extract tables from PDF in Python.
Step 2: Write each table to its own sheet
Now turn each extracted table into a pandas DataFrame and write it to a dedicated sheet in one workbook. The two things that bite people here are Excel’s 31-character sheet-name limit and its rule that sheet names must be unique. Handle both with a small helper.
import pandas as pd
from pdfmux import extract_json
def safe_sheet_name(base: str, used: set[str]) -> str:
"""Return an Excel-legal, unique sheet name (<= 31 chars)."""
# Excel forbids these characters in sheet names.
for ch in r'[]:*?/\\':
base = base.replace(ch, " ")
base = base.strip() or "Sheet"
name = base[:31]
# Ensure uniqueness while staying within 31 chars.
suffix = 2
while name.lower() in used:
tail = f"_{suffix}"
name = base[:31 - len(tail)] + tail
suffix += 1
used.add(name.lower())
return name
def extract_tables(pdf_path: str) -> list[dict]:
result = extract_json(pdf_path, quality="standard")
tables = []
for page in result["pages"]:
for t in markdown_tables(page["text"]):
t["page"] = page["page"]
tables.append(t)
return tables
def pdf_to_excel(pdf_path: str, xlsx_path: str) -> int:
tables = extract_tables(pdf_path)
if not tables:
raise ValueError(f"No tables detected in {pdf_path}")
used_names: set[str] = set()
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
for i, table in enumerate(tables, start=1):
df = pd.DataFrame(table["rows"], columns=table["headers"])
sheet = safe_sheet_name(
f"Table {i} (p{table['page']})", used_names
)
df.to_excel(writer, sheet_name=sheet, index=False)
return len(tables)
count = pdf_to_excel("report.pdf", "report.xlsx")
print(f"Wrote {count} sheets to report.xlsx")
pd.ExcelWriter(..., engine="openpyxl") opens one workbook; each df.to_excel(writer, sheet_name=...) call adds a sheet. index=False drops pandas’ auto-generated row numbers, which you almost never want in a tables export. The result is a single .xlsx file with one tab per source table — three tables in the PDF, three sheets in Excel, all in the right order with their headers intact.
If you only ever have one table per file and want flat output instead, the CSV path is simpler — no sheet-name juggling, no workbook object.
Step 3: Fix numbers stored as text
Every cell came out of the PDF as a string, so Excel will treat your numeric columns as text: left-aligned, un-summable, and unsortable. Convert them with pandas. pd.to_numeric(..., errors="coerce") turns anything that parses into a real number and turns everything else into NaN, so a stray label in a number column never crashes the run.
import pandas as pd
def coerce_types(df: pd.DataFrame) -> pd.DataFrame:
df = df.copy()
for col in df.columns:
# Strip thousands separators, currency symbols, and whitespace,
# and turn accounting-style "(420)" into "-420".
cleaned = (
df[col].astype(str)
.str.strip()
.str.replace(r"[,$£€\s]", "", regex=True)
.str.replace(r"^\((.*)\)$", r"-\1", regex=True)
)
numeric = pd.to_numeric(cleaned, errors="coerce")
# Only adopt the numeric version if most of the column parsed.
if numeric.notna().mean() >= 0.8:
df[col] = numeric
continue
# Otherwise try dates (e.g. "2026-06-15", "15/06/2026").
dates = pd.to_datetime(df[col], errors="coerce", dayfirst=True)
if dates.notna().mean() >= 0.8:
df[col] = dates
return df
The 0.8 threshold is the guard rail: a column only becomes numeric if at least 80% of its values parse cleanly, so a mostly-text column with one stray digit stays text. Drop coerce_types into the Step 2 loop right before writing each sheet:
df = pd.DataFrame(table["rows"], columns=table["headers"])
df = coerce_types(df)
df.to_excel(writer, sheet_name=sheet, index=False)
Now Excel right-aligns the numbers, sums them, and renders the date columns as dates. If you’d rather hand structured, already-typed records to a downstream LLM or pipeline instead of a spreadsheet, PDF to JSON for LLM pipelines covers that shape.
Handling scanned PDFs
If your PDF is a scan — an image with no text layer — pdfmux OCRs the affected pages automatically as part of the same extract_json() call. You don’t switch tools or flags; a scanned bank statement and a born-digital report run through the identical code above. How the OCR engines are selected and chained is covered in OCR PDF extraction in Python.
The confidence score is where the honest limit shows up: pdfmux reports one aggregate score for the whole document, not one per page or per table. A blurry scan or a faint fax can produce a table that looks fine but has a transposed digit, and there’s no per-page score to isolate which sheet it’s in — a low document-level score means review the whole workbook, not just one tab:
result = extract_json("scanned-statement.pdf", quality="standard")
print(f"Document confidence: {result['confidence']:.2f}")
if result["ocr_pages"]:
print(f"OCR was applied on pages: {result['ocr_pages']}")
if result["confidence"] < 0.8:
print("⚠️ Low confidence — review every sheet in this workbook, not just one")
The extract-audit-repair loop that produces that score — extract, audit with quality checks, re-extract failures — is documented in self-healing PDF extraction. Scanned financial documents are the canonical use case; see extract bank statement data in Python for an end-to-end statement workflow.
CSV vs Excel: which should you use
Both start from the same parsed tables list. The choice is about shape and consumer, not about the extractor.
| Question | CSV | Excel (.xlsx) |
|---|---|---|
| Tables per PDF | One flat table per file | Many tables, one workbook |
| Multiple tables | Becomes N separate files | Becomes N sheets in one file |
| Types preserved | No — everything is text | Yes — numbers, dates, formatting |
| Best consumer | Scripts, databases, pandas.read_csv |
Analysts, finance, manual review |
| Dependencies | Standard-library csv |
pandas + openpyxl |
| File size | Smaller | Larger (zipped XML) |
Rule of thumb: if a human is going to open it and look at several related tables together, ship Excel. If a machine is going to load one table, ship CSV. The full CSV walkthrough — including the standard-library-only path — is in PDF to CSV in Python.
Keep reading
- PDF to CSV in Python — the flat-file companion to this post; reach for it when you have one table per file feeding a script or database.
- Extract tables from PDF in Python — how table detection actually works under the hood, with lattice vs. stream structures.
- OCR PDF extraction in Python — how pdfmux OCRs scanned pages automatically and which engines it chains.
- Self-healing PDF extraction — the extract-audit-repair loop behind the document-level confidence score that tells you whether to trust the workbook.
Last updated: June 2026.