A PDF doesn’t contain a table. It contains instructions to draw text at certain positions, plus some lines; the table is something your eyes put together. When a search system pulls the text out of a PDF, it usually gets the words in roughly the right order and loses which column each one was in. Here you’ll take a real two-page leaflet apart four ways, see what each one makes of its tables, and ask a model questions about prices and timetables. Extract tables as tables, with their rows and columns kept, or the model is left guessing.
What’s inside a PDF#
Riverside Leisure Centre’s autumn guide is a made-up leaflet we built with a PDF library, with three tables of the kinds real documents have: membership prices with a heading spanning two columns and some empty cells, a class timetable that’s mostly gaps, and pool opening times with two sessions in one cell. You can download the PDF and try your own tools on it.
We tried four ways of turning it into text:
- Plain text, with pypdf, a popular library that many document loaders use: the words in reading order.
- Layout text, with pdfplumber’s layout mode, which adds spaces and blank lines to keep words roughly where they were on the page.
- Tables as Markdown, with pdfplumber’s table finder, which uses the lines drawn around cells to rebuild each table row by row, empty cells included. Each table becomes a Markdown table under its heading; the rest of the page stays plain text.
- Rows as sentences: the same tables, with each row written out as one line that names every column and says “none” for an empty cell: “Student: Off-peak monthly: £16.00; Off-peak annual: none; …”.
Predict first
In the price table, a student’s row reads £16.00, (empty), £22.00, (empty), £0.00. In pypdf’s plain text, what does that row look like?
Plain text keeps the words and drops the grid. Every value sits on its own line, a row’s values follow its name, and an empty cell leaves no trace, so after a gap every value slides one column left. The timetable suffers most: the 09:30 row comes out as “Pilates, Aqua fit, Pilates”, with nothing to say the second Pilates is on Thursday, not Wednesday. Layout text keeps the gaps as spaces, which a person can line up by eye, but a cell with two lines splits its row in two. Markdown keeps every row together with an empty slot for every empty cell, and the heading that spanned two columns is folded into each column’s name: “Anytime monthly”. Plain text drops empty cells without a trace, and with them which column every later value belongs to.
Ask the leaflet#
Now the test that matters. Fifteen questions a visitor might ask, each answered by Qwen3-1.7B, a small open model, from each extraction. It gets either the two chunks most similar to the question, as a search system would give it, or the whole leaflet, which here is short enough to fit.
Predict first
From plain text, given the two chunks most like each question, how many of the 15 does the model get right?
The table under the figure has all eight results. Plain and layout text manage 2 to 5 of 15 whichever way the model gets them. Markdown tables get 9 from the top 2 chunks, and rows written as sentences 11: when every value sits next to its row and column names, there’s nothing to line up. Giving the model the whole leaflet didn’t help the good extractions; it gave the model more to misread. The questions that beat every method are the ones whose answer is an empty cell: whether students can pay annually, or swim lanes on a Sunday. Faced with a gap, the model reaches for a nearby value. Turn tables into text a model can’t misread: every value next to its row and column, and empty cells said out loud.
Getting tables out in practice#
- Look at what your loader produces. Print the extracted text of a page with a table before you build anything on it. If you can’t answer a question from it, neither can the model.
- Use a table-aware extractor. pdfplumber and Camelot can find tables from the lines drawn around cells; tools such as Unstructured and LlamaParse can also detect tables without them. Some tools read the page as an image with a vision model, which helps with scans.
- Keep each table as one chunk, with its heading and page number, so search returns the whole table, header row included. For a small model, write each row as a line that names every column, with empty cells as “none”.
- Check the tricky cells: merged headings, empty cells and cells with two lines are where extractors slip, so test a few by hand.
In code#
The table finder and the Markdown conversion, folding the spanning heading into the row below, run on the leaflet (pip install pdfplumber pypdf):
import pdfplumber, pypdf
def to_markdown(rows):
"""A table as Markdown, keeping empty cells. A heading that spans columns
(pdfplumber gives None under it) is folded into the heading row below."""
rows = [[c.replace("\n", " / ") if c else c for c in r] for r in rows]
if None in rows[0]:
span, top = "", []
for c in rows[0]:
span = c if c is not None else span
top.append(span)
rows = [[f"{t} {b.lower()}".strip() if t else b for t, b in zip(top, rows[1])]] + rows[2:]
lines = ["| " + " | ".join(rows[0]) + " |", "|" + "---|" * len(rows[0])]
return "\n".join(lines + ["| " + " | ".join(c or "" for c in r) + " |" for r in rows[1:]])
plain = pypdf.PdfReader("riverside-leaflet.pdf").pages[0].extract_text()
print(plain[plain.index("Adult"):plain.index("Over 65")]) # what most loaders hand to the model
with pdfplumber.open("riverside-leaflet.pdf") as pdf:
table = pdf.pages[0].find_tables()[0]
print(to_markdown(table.extract()))
# Adult
# £24.00
# £240.00
# £36.00
# £360.00
# £20.00
# Student
# £16.00
# £22.00
# £0.00
#
# | Membership | Off-peak monthly | Off-peak annual | Anytime monthly | Anytime annual | Joining fee |
# |---|---|---|---|---|---|
# | Adult | £24.00 | £240.00 | £36.00 | £360.00 | £20.00 |
# | Student | £16.00 | | £22.00 | | £0.00 |
# | Over 65 | £18.00 | £180.00 | £27.00 | £270.00 | £10.00 |
# | Family (2 adults, 2 children) | £52.00 | £520.00 | £78.00 | £780.00 | £30.00 |
# | Junior (under 16) | £10.00 | £100.00 | | | £0.00 |What to remember
- A PDF stores text at positions, not tables; plain-text extraction keeps the words and loses the columns, and empty cells vanish without a trace.
- With the top 2 chunks, a small model got 2 of 15 questions right from plain text, 9 from Markdown tables and 11 from rows written out with every column named.
- Extract tables with a table-aware tool, keep each as one chunk in Markdown or CSV with its heading, and look at the output before you trust it.
