Skip to content
New — orchestrate a team of Mel agents from your terminal. Learn more about orchestrating Mel agents
← All guides

Guides · PDFs

How to pull tables or totals out of many PDFs

By Lalla Asmaa Alaoui, founder of Mel · Updated

Turn each PDF into text with a tool such as pdftotext, pull out the values you need with a search or a short script, and write them into one CSV file with a row per PDF. With an AI agent, you point it at the folder, describe the columns you want, and check a few rows against the original PDFs.

This works well for invoices, statements, reports and forms that follow the same layout. Scanned PDFs are pictures of text, not text, so they need OCR first. Whatever method you use, spot-check the results before you rely on them.

Steps by hand

  1. Copy the PDFs into one working folder. Work on copies, not on the only originals.
mkdir -p ~/invoices-work && cp ~/Downloads/invoices/*.pdf ~/invoices-work/
cd ~/invoices-work
  1. Install a PDF-to-text tool. pdftotext is part of the Poppler tools. - Mac with Homebrew: brew install poppler - Debian or Ubuntu: sudo apt install poppler-utils - Windows: Poppler is available as a Windows build; add its bin folder to your PATH.
  1. Look at one PDF as text first.
pdftotext -layout invoice-001.pdf -

-layout keeps columns lined up, which helps with tables. The - prints the text to the terminal instead of a file. If nothing prints, the PDF is probably a scan and needs OCR.

  1. Convert every PDF. On Mac and Linux:
for f in *.pdf; do pdftotext -layout "$f" "${f%.pdf}.txt"; done

In PowerShell:

Get-ChildItem *.pdf | ForEach-Object { pdftotext -layout $_.FullName ($_.FullName -replace '\.pdf$', '.txt') }
  1. Find the line you need. For example, the total on each invoice:
grep -H "Total" *.txt

On Windows, findstr "Total" *.txt does the same. Look at how the line is written, because that decides your pattern.

  1. Write the CSV with a short script. This Python example takes the first amount after "Total" in each file. Save it as totals.py in the folder:
import csv, glob, re

rows = []
for path in sorted(glob.glob("*.txt")):
    text = open(path, encoding="utf-8", errors="replace").read()
    m = re.search(r"Total\D*([\d.,]+)", text)
    rows.append({"file": path, "total": m.group(1) if m else ""})

with open("totals.csv", "w", newline="") as f:
    w = csv.DictWriter(f, fieldnames=["file", "total"])
    w.writeheader()
    w.writerows(rows)
print(f"{len(rows)} rows written to totals.csv")

Run it with python3 totals.py on Mac and Linux, or python totals.py on Windows.

  1. Check the gaps. Open totals.csv in a spreadsheet. Empty cells mean the pattern did not match that file. Open those PDFs and adjust the pattern.
  1. Spot-check. Pick five rows at random and compare them with the PDFs by eye. Number formats such as 1.234,50 and 1,234.50 are a common source of errors.

For real tables with many rows per PDF, a Python library made for PDF tables is easier than patterns. Either way, the check in steps 7 and 8 stays the same.

Doing it with Mel

Mel's agent can do this job in your folder. Open the folder in a Mel pane, press ctrl+enter (cmd+enter on macOS) and describe the result you want:

This folder has about 200 invoice PDFs. Make one CSV, invoices.csv,
with one row per PDF and these columns: file, invoice number, date,
supplier, total, currency. Leave a cell empty if you cannot find a value,
and list those files at the end. Do not change or move the PDFs.

The agent can check which PDF tools are on your machine, write a small script like the one above, run it, and fix the pattern for the files it missed. With Mel's approvals, each command and each file it writes shows on an approval card first, so you can see what it will run. When it is done, ask it to show you five random rows next to the text it read from those PDFs, then check a few yourself.

For one document, you can also attach a PDF to your message with the + button in the agent input. Today, attached PDFs go to Claude models; on Auto, Mel routes the turn to a model that can read the document. The agent can also write its findings into a styled PDF report for you with its PDF tool.

FAQ

How do I extract data from multiple PDFs into Excel? Convert each PDF to text, pull the values with a pattern or script, and write a CSV. Excel and other spreadsheets open CSV files directly.

Why does pdftotext return nothing? The PDF is probably a scan, which holds images rather than text. Run OCR on it first, then extract.

Can AI read tables in PDFs accurately? Often, but not always. Treat the first result as a draft: look for empty cells, and compare random rows with the original PDFs.

Is it safe to send my PDFs to an AI model? What the agent reads is sent to the model provider you use. Check that provider's data terms, and keep confidential documents to providers and plans you trust.

What is the best format for the output? CSV is simple and opens everywhere. Keep one row per PDF and a column with the file name, so every value can be traced back.

Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla Asmaa Alaoui
Written by
Lalla Asmaa Alaoui
Founder & CEO of Mel

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.

Follow on LinkedIn

Get started

Try the agentic terminal.

The Mel terminal is free to download, no account needed. The AI agent is free on your own provider key — 1,500 hosted-AI credits a month on Pro.