Tables, PDFs and messy documents
Real documents are scanned, multi-column, full of tables and images. Most RAG failures start here, before any search happens.
In 60 seconds
Tables, PDFs and messy documents
Real documents are scanned, multi-column, full of tables and images. Most RAG failures start here, before any search happens.
Why PDFs are hard
| Problem | What you see after extraction | What helps |
|---|---|---|
| Two or more columns | Lines from both columns mixed together | Layout-aware extraction; check reading order on sample pages |
| Headers, footers, page numbers | "Page 7 of 40 · Confidential" inside every chunk | Detect lines repeated on most pages and remove them |
| Words broken across lines | "applic- ation" | Rejoin hyphenated line breaks carefully |
| Special characters and ligatures | "file" instead of "file", odd symbols | Unicode normalisation (for example NFKC) and a cleaning step |
| Indian-script PDFs with older font encodings | Garbled characters that look fine on screen | Check a sample; OCR the rendered page if the text layer is unusable |
| Scanned pages | No text at all, only an image | OCR (next section) |
Scanned documents and OCR
- 1
Detect
Check each page: does it have a usable text layer, or is it an image? - 2
Prepare the image
Straighten, crop and improve contrast; use a good resolution. - 3
Run OCR with the right language
Tell the engine which script and language to expect. - 4
Check confidence
Flag pages with low confidence or strange characters for review. - 5
Store the source
Keep the page image link, so users can see the original when OCR is doubtful.
Tables
- Keep a table together when it is small, and store it in a clear text form such as Markdown with the header row.
- For big tables, repeat the header with every group of rows, or turn each row into a sentence.
- Add the table's title and unit ("Fees in ₹ per semester") to every table chunk.
- For numbers people will filter, sum or compare, also load the table into a database. The structured retrieval lesson covers this.
def rows_to_sentences(title, header, rows):
"""Each row becomes one line that makes sense on its own."""
out = []
for row in rows:
pairs = [f"{col}: {val}" for col, val in zip(header, row)]
out.append(f"{title} | " + "; ".join(pairs))
return out
header = ["Course", "Tuition (Rs per semester)", "Hostel (Rs per semester)"]
rows = [["B.Sc. Physics", "18,000", "22,000"], ["B.Com", "15,000", "22,000"]]
for line in rows_to_sentences("Fee schedule 2025-26 (made-up)", header, rows):
print(line)Images, charts and diagrams
- One extraction setting for every file type
- Nobody reads the extracted text
- Tables flattened into lines of numbers
- OCR output indexed with no quality check
- Different handling for text PDFs, scans, slides and spreadsheets
- Random samples read by a person after every change
- Tables kept whole or turned into self-contained rows
- Low-confidence pages flagged and linked to the original image
Watch and read more
Lab
An extraction quality report on three of your own documents, with fixes for the worst problems.
The problem
Pick three documents you are allowed to use: one normal text PDF, one with a table, and one scan or photo of a printed page (for example a public notice). Do not use private documents of other people.
- Extract the text with any tool you have: copy-paste, a free PDF library, or a free OCR tool.
- For each document, list every problem you see: order, headers, broken words, characters, tables, missing text.
- Rewrite one table using the
rows_to_sentencesidea from the lesson. - For each problem, write the fix you would build, and how you would detect the problem automatically across many files.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1How could you detect page headers and footers automatically across a 300-page PDF?Reveal
Extract the first and last few lines of every page. Normalise them by removing digits (so "Page 7" and "Page 8" look the same) and extra spaces. Lines that appear on most pages are almost certainly headers or footers, so remove them before chunking. Check a sample by eye, because a real heading might repeat on a few pages too.
Q2A scanned Marathi notice is OCRed with many errors. Keyword search on the OCR text fails, but vector search sometimes works. Why, and what would you do?Reveal
Keyword search needs exact word matches, and OCR errors break words, so it fails. Vector search can still catch the general meaning from the words that survived. Improve OCR first: better scans, the correct language setting, image clean-up, or a different engine or vision model. Flag low-confidence pages, and always show the original image as the source so users can read the real text.
Questions people ask
Which PDF library should I use?
There are many open-source options, and they differ in speed, layout handling and table support. Try two or three on a sample of your documents and read the output. Tools change often, so check current docs and licences.
Should I always OCR, to be safe?
No. If a PDF has a good text layer, extracting it is usually more accurate than OCR. OCR is for scans, and for pages whose text layer is broken, such as some older Indian-script PDFs.
How do I handle Word files, slides and spreadsheets?
Use a reader made for each format, so you keep headings, slide titles and cell structure. Spreadsheets are often better loaded into a database than turned into text.
How much time should go into document processing?
Often more than people expect. For many real projects, cleaning and structuring documents is the single biggest factor in answer quality. Budget for it from the start.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.