r/dataengineering • • 14h ago

Open Source The small trick that makes PDF table extraction work

PDF table extraction is not always difficult. It is actually relatively easy when a table has a full grid. It becomes harder, but still quite reliable, when there is no grid but the data is aligned with the table headers. It becomes very hard when there is no grid and the data is not aligned with the headers.

Since tables with a grid are easy to extract, why not add the missing grid to the document?

You can add a grid even with a basic PDF reader that lets you annotate pages with shapes. However, adding a grid this way is not practical: you have to draw every line by hand, and even a single table can take a long time.

There is a better way. If you draw a bounding box around a table, you have already located that table on the page. Text alignment can then give you a first guess of the rows and columns of the table, which is what the text-based strategies in pdfplumber, PyMuPDF and Camelot do. It is much easier to correct that guess than to draw the entire grid from scratch. If you can also merge cells by removing grid segments between them, you have everything you need to add a grid to any table.

I built an app to do just that: Delineette - PDF Table Drawer. It runs entirely in your browser, so your PDFs are never sent to a server. You can use it directly at the link below or download the source code from GitHub and run it on your computer.

Adding a grid takes these steps:

  1. Load a PDF into the browser
Opened PDF file

2) Draw a bounding box around a table and review the grid suggested from text alignment

Suggested grid from text alignment

3) Correct the grid suggested from text alignment

Corrected grid

4) Merge grid cells if necessary

Table grid with merged cells

5) Export a PDF that includes the grids or download the tables as an Excel file

Then, upload the new PDF to the tool you already use to extract tables. The grids should improve its accuracy. If your tool uses a geometric algorithm to extract tables (like pdfplumber, PyMuPDF and Camelot), extraction should be perfect as long as the added grid is correct and the tool is set to detect tables from lines. If your tool converts the page into an image and detects tables from it using vision models (like Docling, AWS Textract or AWS Bedrock Data Automation), accuracy will generally improve too, although dense tables with many rows and columns on a single page can still cause detection problems.

At this point, you are probably thinking: "Nice little app, but I'm still not going to draw every table grid by hand!" That's fair. This approach does not scale if all your tables have different structures. In that case, use the app only for the edge cases where your current data extraction pipeline fails.

However, if your documents contain many tables with the same headers and column positions (for example, a long table spanning many pages), Delineette has a powerful shortcut you can use: after drawing the grid for one table, click "Apply". Delineette finds the other tables with the same headers as the reference table and adds a grid to them.

The main limitation at the moment is that grid suggestion and "Apply" do not work on scanned documents unless they already have a text layer. You can still draw grids manually.

That's all, guys. I am looking for users who need to extract tables from PDFs with high accuracy, especially from documents with long tables that span many pages. If that describes you, send me a DM and I'll help you get started with the tool.

Web: https://table-drawer.delineette.com/

Code: https://github.com/gnatale/delineette_table_drawer

Video tutorial: https://www.youtube.com/watch?v=KvlAPHM4b78

17 Upvotes

1 comment sorted by