Wringing Data Out of PDFs

What happens if the data you need to analyze comes in PDF forms? This is what I tackled and solved last week. I wrote recently about the frequent need to work with data in the format it comes, but the examples in that blog were already spreadsheet data albeit requiring rearrangement to ingest for analysis. PDF’s add a different hurdle to the task. Do you have situations like this with your data? Reach out if I can help (and read on for a few details about this “PDF Parsing” solution).

A client of mine gets auto-generated monthly PDF reports on her clients’ businesses where each report consists of numerous pages. The pages give the overall summary (Page 1) followed by breakdowns by business sub-categories (thankfully with the same format on each page). No matter how familiar such a report might get, it is easy to miss trends in the data. Getting just PDF tables each month means no ability to graph or re-summarize the data; data are scattered across the report’s pages making it difficult to hold a whole picture in mind. The format also makes it impossible to combine data across months or across businesses to see bigger trends. Yes. We are working on getting it as spreadsheet data, but in the mean time…

To illustrate, here is a PDF page mockup for a hypothetical, Boulder CO store where each page is the data on a product type like Jackets, Camping Gear etc. My solution for this translates the multi-page PDF’s like this into spreadsheet tables that can be used for whole business summaries like the Sales Summary example below or for graphing the data to uncover trends (my mockup PDF has two pages. The real data data are 40+)

PDF Report Page (Page 1 of 40+)

Example Report Output

Technical Learning from Creating this Solution

AI Tools Are Getting Better and Better

When used within a controlled work process (and that’s a very important caveat!), current AI tools are reaching amazing levels. I attacked and solved the above problem with Claude Code. Within a few hours, with me guiding and approving and it doing the legwork, I added PDF as a supported file format in my toolbox. Claude and I created the Trailhead mockup. We then validated a demonstration project of extracting its data to an overall Dataframe and summaries. Importantly, the scripts are generically configurable and work with my client’s more complex report format that contains 500+ values per page versus 20 or so for the mockup. Per the caveat, AI would give nothing but dysfunction here without the strong guidance of being fed references including my toolbox code and Claude skills describing my extensive general expectations and example code style for projects.

My “How I AI” System Makes it Go

To kick this off, I fed it a prompt with a few sentences describing the goal and an example of my client’s actual, 48-page report with its 20,000+ values to import and reshape. I asked it to author an overall “Change” document per the template in my “python-project-changes” AI skill. Claude suggested an 8-part plan including the Trailhead mockup. We tweaked this a bit and then worked each of the 8 parts through using my “How I AI” system that allows me to collaboratively work with it and review what it does and how it does it.

Open-Source Wins Again

Claude brought the PDFPlumber open–source code library to my attention and we did a quick exploration with that on the full-scale report. That was convincing that PDFPlumber could use the colored, header blocks on a form to locate sub-tables. As an aside, there are other wayfinding options in reserve such as using specific words. Here is the Trailhead page with its identifying highlights from the PDFPlumber layout. Claude wrote a go-between pdfplumber_tools.py module to add to my toolbox. It accesses PDFPlumber and honors my toolbox’ existing architecture that imports and then parses raw data.

The resulting, raw, parsed table is something that can be adapted to any data analysis and visualization purpose. It uses page_number as a key and merges in category from the page header. The sharp-eyed reader will note that we created a solution for merged cells such as the “Floor Staff” example in the bottom table. This overall table is a jumping off point for subsetting and pivoting to look at category highlights, sales trends and staffing allocations.