New Tool Radar

AI PDF

How to Extract a Table from an OCRed PDF with pdfplumber

Extract a table from a scanned PDF with pdfplumber after OCR has added the text layer it needs.

Schematic illustration of pdfplumber extraction: OCR the scan first, extract the table, and save it to a spreadsheet.

pdfplumber works at the level of the PDF geometry: it reads characters, lines and rectangles, and you direct how it finds the table. It works best on machine-generated PDFs, not scans, so a scan must be OCRed first. This guide extracts one table that way.

Prerequisites

Python, pdfplumber installed, OCRmyPDF for the OCR step, and a scanned PDF with a table. Work on a copy.

Steps

1. OCR the scanned PDF first

Run OCRmyPDF on the scanned PDF to add a text layer. Expected result: the table text becomes selectable in the output PDF.

2. Open the PDF and extract the table

Open the OCRed PDF with pdfplumber and call extract_table on the page. Expected result: a list of rows is returned, with one row per table row.

3. Check the table against the printed page

Compare the returned rows with the printed page. Expected result: the three rows match the printed page.

4. Save the rows to a spreadsheet

Write the rows to a spreadsheet file. Expected result: a spreadsheet is created with the three rows.

Practice check: fictional book order, three-row table

The table below is fictional practice material. It lists the three rows pdfplumber must return.

Item Amount
Novel, 1 copy 14.99
Guide, 1 copy 9.99
Atlas, 1 copy 24.50
Total 49.48

Expected result. pdfplumber returns the three rows, which add to 49.48, and the spreadsheet holds the same three rows.

Limits

  • pdfplumber reads the text layer, not the image; a scan that OCR read badly gives it bad text, so check the OCR layer first.
  • UNTESTED – no OCR tool or AI tool was run; all results above are expected results. The practice file is fictional.

Sources

See all