I’m trying to pull text out of a 40-page scanned contract with Python, but the libraries I found only return empty strings. How do people usually learn which one handles scanned pages?
I agree this is annoying. Honestly, scanned pages are just images, so text extraction libraries return empty strings because there is no text layer. I would open the file in a regular viewer and try to select a word. If that fails, you need OCR rather than another extraction library, so I would look at Tesseract, often used through pytesseract in Python.
Empty strings mean the PDF has no text layer, it’s just page images, so pypdf or pdfplumber can’t grab anything. You need OCR. Convert each page with pdf2image, then run pytesseract on it. That combo handles a 40-page contract fine, it’s what I’d reach for.
Scanned pages are just images, so normal PDF text libraries return nothing; I’d run the file through OCRmyPDF with Tesseract first, then use a library like pdfplumber on the searchable copy. Start with 2 or 3 pages to check the OCR, and avoid expecting table-heavy contract pages to come out perfectly first time.
For scanned pages, the PDF usually contains images rather than selectable text, so you need OCR before Python can extract anything useful. I would first test 1 page and check whether you can select text in a normal viewer. Avoid judging results only by the raw text, honestly, because contract columns and signatures can scramble the reading order.
@cameron2002 That is such helpful advice for those stubborn contracts! I always panic when vendors send me image-only schedules becuase the margins get so messed up when copying text. What kind of device are you using to run those scripts?
@guitar_mouse That matches what I see with old history handouts: the text comes out in odd order once columns are in play. I’d check each page against the original before trusting the extraction, and keep a backup copy of the scan untouched. If a page looks jumbled, read it back visually rather than fixing the script straight away.
Scanned pages are just images, so regular text libraries like PyPDF2 won’t find anything since there’s no actual text layer to grab. You need OCR, I’d look into pytesseract with pdf2image, it actually reads the picture.