# PDF Tables to CSV with PyMuPDF: Check the Cells Before Using AI

> Extract a ruled table from a text PDF into a page-labeled CSV and audit header changes and numeric associations before using it in an AI workflow.

Canonical article: https://modelrovia.com/guides/pdf-table-to-csv-for-ai/
Author: Modelrovia Editorial
Published: 2026-11-09
Updated: 2026-11-09

## Key takeaway

Export each table with its source page, then check the column meanings and numeric cells against the PDF.

Use this workflow when a text PDF contains a table you need to give to an AI assistant. Export the cells first and preserve their page number. In our one-page fixture, the extractor kept all three plan rows and the numeric values, but compressed spaces in two headers. That visible defect is exactly why an export needs inspection before it becomes evidence for a generated answer.

## Reproduce the extraction

Download the [fictional plan table PDF](/examples/pdf-plan-limits.pdf) and the [table export script](/examples/pdf-table-to-csv.py). The script adapts the page-level table detection demonstrated in PyMuPDF's official recipes and notebooks. It writes each detected table separately and keeps its page number in the filename.

Run the commands in a fresh Python 3.12 virtual environment. The final argument must be a new output directory.

~~~sh
python -m pip install pymupdf==2.1
python pdf-table-to-csv.py pdf-plan-limits.pdf extracted
~~~

We ran this on November 9, 2026 with PyMuPDF 2.1 on macOS. The script reported four rows including headers in page-1-table-1.csv. Download the [actual CSV](/examples/pdf-plan-limits-extracted.csv) to compare your output.

~~~csv
Plan,Exportspermonth,ExtraseatUSD
Cedar,120,8
Birch,450,12
Spruce,900,15
~~~

## Validate values and units separately

Open page 1 beside the CSV. Check that Birch still has 450 exports and an extra-seat price of 12 USD. Then verify that the first numeric column is a monthly allowance and the second is a price per extra seat. A model can read the number 450 perfectly while assigning it the wrong unit.

For this fixture, an explicit mapping from Exportspermonth to Exports per month and ExtraseatUSD to Extra seat USD is safe after checking the original. For a folder of documents, keep the original header, approved header and source page as separate fields. A rule that inserts spaces into every camel-case or joined header can change identifiers that were intentionally written without spaces.

## Pass only checked rows to the assistant

For a question such as “What is Birch's extra-seat cost?”, supply the CSV and a small note containing the approved headers and page reference. Ask the assistant to report the row and unit and leave any missing value unknown. The expected answer is 12 USD per extra seat, supported by the Birch row on page 1. This expected answer comes from the fixture, independently of any model output.

## Recognize when this route runs out of evidence

The test uses a text layer and clear rules around the table. A scan may require OCR first. Borderless tables may need a different detection strategy, and a table that spans two pages may need manual header alignment. If detection returns zero tables, check whether you can select the table's text in the PDF and see actual cell boundaries. For an OCR-first route, see [Mistral OCR's export workflow](/guides/mistral-ocr-pdf-markdown/). For whole-document structure, see [MinerU PDF to Markdown](/guides/mineru-pdf-to-markdown/).

## Sources & references

- [PyMuPDF text and table recipes](https://pymupdf.readthedocs.io/en/latest/recipes-text.html)
- [Official table-analysis notebooks](https://github.com/pymupdf/PyMuPDF-Utilities/tree/master/table-analysis)

[Editorial standards](https://modelrovia.com/methodology/)
