Getting data out of a Nepali PDF with a broken text layer
· 1 min read
I was handed a 373-page PDF of records in Nepali and asked for a spreadsheet. Easy, I thought: extract the text and parse the tables.
The text came out as nonsense. The PDF had been generated with a broken font mapping, so the characters stored in the file didn't match the Devanagari you see on screen. Every extraction tool I tried gave the same garbled output.
Plan B: read it like a human would
If the text layer lies, ignore it and read the pixels:
- Render every page as an image at 300 DPI.
- Run Tesseract with the Nepali language model, keeping a confidence score for every word.
- Parse the results with a small state machine that remembers which province, district and constituency it's in as it moves down each page, so every row ends up in the right place.
Don't guess
OCR is never perfect. Instead of quietly "fixing" uncertain values, I put every low-confidence row on a separate needs review sheet. Where a ward number was unreadable, I left it blank. Guessing it from the rows around it turned out to be wrong far too often.
In the end there were 6,557 records in a clean workbook, plus a short list of rows for a person to check. That's a much better result than a perfect-looking file that's quietly wrong.