state of affairs: You’re employed within the operations staff of a medium-sized firm. Day-after-day, your staff processes order varieties from completely different B2B prospects. All of them arrive as PDFs. And in idea, all of them include the identical data: buyer ID, buy order quantity, supply date, and the ordered gadgets.
In apply, nonetheless, each doc seems barely completely different: One buyer locations the acquisition order quantity within the top-left nook, the subsequent one within the bottom-right nook. Some write “PO Quantity”, others use “Order ID”, “Order Reference”, or one thing fully completely different.
For us people, that is normally not an issue. We take a look at the doc, perceive the context, and instantly acknowledge which data is supposed.
For conventional automation programs, nonetheless, this turns into troublesome: A regex rule can particularly seek for “PO Quantity: “. However what occurs if the subsequent buyer makes use of “Order Reference: “ as an alternative?
That’s precisely the issue I recreated for this text.
We evaluate two completely different approaches for extracting structured information from B2B order varieties:
- A conventional rule-based strategy utilizing pytesseract and regex guidelines
- An LLM-based strategy utilizing pytesseract, Ollama, and LLaMA 3
The purpose of this text is to not present that LLMs are usually higher. They aren’t at all times.
A way more attention-grabbing query is: At what level do conventional extraction pipelines begin to attain their limits as complexity and the variety of completely different layouts enhance? And when can an LLM truly scale back upkeep effort?
Desk of Contents
1 – Step-by-Step Information
2 – Head-to-Head Comparability
3 – When ought to we NOT use an LLM?
4 – Last Ideas
The place to Proceed Studying?
1 – Step-by-Step Information
We rebuild each approaches step-by-step. First, we create two pattern PDFs containing the identical enterprise data however utilizing completely different layouts. Afterwards, we extract the information as soon as with a conventional OCR and regex pipeline and as soon as with an OCR and LLM pipeline. This enables us to check each approaches underneath similar circumstances.
- The standard strategy principally asks:
“Can I discover the precise sample that I programmed?” - The LLM-based strategy as an alternative asks:
“Can I perceive the that means of this area in context?”
→ 🤓 Discover the complete code within the GitHub Repo 🤓 ←
Earlier than We Begin — Mise en Place
pip vs. Anaconda
On this information, we use pip, Python’s normal package deal supervisor. This implies we set up all libraries straight by way of the command line utilizing pip set up …. pip is already included routinely once you set up Python. If you realize Python tutorials that work with Anaconda, that’s merely one other solution to obtain the identical purpose (utilizing conda set up …). Within the article “Python Knowledge Evaluation Ecosystem — A Newbie’s Roadmap”, you could find additional particulars about getting began with Python. Moreover, on a Microsoft system we use the CMD terminal (Home windows key + R > click on on cmd).
Create and activate a brand new digital surroundings
Create a brand new python surroundings with python –m venv b2bdocumentextractor (you may change the title) in a terminal and activate it withb2bdocumentextractorScriptsactivate.
Optionally available: Test Python and pip
python --version
pip --version
You must see a Python and a pip model.
Step 1 – Set up Tesseract
Tesseract is the OCR engine. It’s the device that truly reads textual content from photographs or scanned PDFs utilizing OCR (Optical Character Recognition). pytesseract is just the Python bridge to Tesseract. This implies: Our Python code can talk with Tesseract by way of pytesseract, however the actual textual content recognition is finished by Tesseract itself. With out putting in Tesseract first, pytesseract can’t work.
First, we obtain the most recent .exe-file for w64 and run the installer:
GitHub – Tesseract at UB Mannheim
Necessary: Keep in mind the set up path:
C:Program FilesTesseract-OCR
Contained in the CMD terminal, we confirm the set up utilizing the next command:
"C:Program FilesTesseract-OCRtesseract.exe" --version
If all the pieces labored accurately, we should always see the corresponding Tesseract model.

Step 2 – Set up Poppler
Subsequent, we set up pdf2image. That is our library for changing PDFs into photographs and it requires Poppler within the background. Poppler is an open-source PDF rendering library used to show PDF information.
For this, we obtain the most recent model of Poppler, extract the ZIP file, and transfer the extracted folder to the C: drive.
GitHub-Poppler Home windows Releases
Contained in the folder, click on on Library > bin and save the trail the place you saved the folder in your C: drive. On my machine, it seems like this:
C:Usersschuepoppler-26.02.0Librarybin
Moreover, we add the trail to the PATH variable so Home windows is aware of the place Poppler is positioned.
Trace for Newbies:
Press the Home windows key and seek for Edit surroundings variables. Afterwards click on on Edit the system surroundings variables. Then click on on Surroundings Variables. Below Person variables, choose the variable PATH, click on on Edit, then New, and paste the trail.
Now restart CMD so the adjustments are utilized.

Step 3 – Set up Python Libraries
Now we set up all Python libraries we’d like. Ensure you reactivate the Python surroundings beforehand:
- pytesseract: We set up this library because the bridge between Python and Tesseract. We already put in Tesseract because the OCR engine, however solely with pytesseract can Python talk with it straight.
- pdf2image: pytesseract is an OCR engine, which suggests it acknowledges textual content from pixels in a picture. It can’t learn PDF constructions straight. pdf2image due to this fact performs an intermediate step: It renders every PDF web page as a picture, just like a screenshot, in order that pytesseract can analyze it afterwards. Word: If we had digital PDFs (that means PDFs the place you may choose and replica textual content), we may straight extract the textual content utilizing libraries comparable to pdfplumber or PyMuPDF. Nevertheless, since we assume that B2B order varieties are sometimes scans in apply, we take the detour by way of pdf2image.
- pillow: pdf2image and pytesseract use this image-processing library within the background (we don’t straight see the utilization within the code) to accurately course of photographs.
fpdf2: We use this library to routinely generate two take a look at PDFs (Format A and Format B) by way of script for the article instance.
ollama: This library permits our Python script to ship messages to the LLM and obtain responses.

Step 4 – Set up Ollama and Obtain LLaMA 3
As soon as the set up of the libraries labored efficiently, we set up Ollama and LLaMA 3 because the LLM. Ollama is the device that permits us to run LLMs fully free, domestically on our laptop computer, and with out API keys.
First, we set up Ollama. When you’ve got not already carried out this, you may obtain the Home windows installer from Ollama and execute it.
Afterwards, we obtain LLaMA 3 utilizing the next command:
ollama pull llama3
Relying in your web connection, this step might take a while since roughly 4.7 GB are downloaded. Nevertheless, we will see a progress bar within the terminal.

Afterwards, we confirm whether or not all the pieces labored:
ollama listing
In case you see one thing just like the screenshot, it labored efficiently.

Step 5 – Create the Undertaking Folder and Generate Take a look at PDFs
For this comparability, we create two B2B order varieties for Alpha GmbH and Beta AG that include the identical data however use completely different layouts. On this instance, we assume that the order varieties are scans, which is why we beforehand put in pdf2image (for digital PDFs, this might even be attainable with libraries comparable to pdfplumber or PyMuPDF).
First, we create a challenge folder to retailer all information there:
mkdir document_extractor
cd document_extractor
Subsequent, we create a brand new file known as create_test_pdfs.py and insert the next code that you could find on this GitHub-Gist. We save this file contained in the beforehand created folder document_extractor:
https://gist.github.com/Sari95/a52a62eb78e0604c4d8c64f5cdd1160a
Now we return to the terminal and execute the file:
python create_test_pdfs.py
Contained in the folder, we will now see the 2 newly created PDFs:

Within the two PDFs, we will already see the issue:
- They include the identical data.
- However the PDFs use fully completely different area names and a special date format.
Strategy 1: The Conventional Manner (pytesseract + Regex Guidelines)
The standard strategy works in two steps:
- First, we convert the PDF into a picture. Afterwards, we use pytesseract to learn the picture and extract the uncooked textual content by way of OCR (Optical Character Recognition). Put merely, OCR implies that the device “seems” on the picture and tries to acknowledge letters from pixels. Fairly just like how people decipher handwritten notes.
- Within the second step, we use regex. These are common expressions that seek for particular patterns contained in the textual content. For instance, we will outline: “Seek for all the pieces that comes after
PO Quantity:.”
Already on this second step, we will establish the primary drawback: What occurs if the shopper merely writes “Order Reference” as an alternative of “PO Quantity: “?
In that case, the regex sample finds nothing. What we will then do (or should do) is add a brand new rule.
Execute Script 1 for Strategy 1
Subsequent, we create a brand new file known as approach1_traditional.py with the next code that you could find within the GitHub-Gist inside the identical folder:
https://gist.github.com/Sari95/aa2be6938fbcb1c7f94b053d9046f55d
Now we execute the file once more contained in the terminal:
python approach1_traditional.py
The Results of Strategy 1
For Format A, all the pieces works completely:
For Format B? Not a single area is acknowledged and all values return “None”:

And that is precisely the place the issue lies. For each new buyer, new regex guidelines must be written, examined, and deployed. With 200 prospects, which means 200 completely different patterns. And each time a buyer barely adjustments their kind, the system breaks once more.
Strategy 2: A New Manner (pytesseract + Ollama + LLaMA 3)
On this second strategy, we preserve the OCR step, however substitute the inflexible regex guidelines with an LLM:
- pytesseract nonetheless reads the textual content from the PDF.
- As a substitute of telling the code “Seek for PO Quantity: ”, we inform the LLM: “Right here is an order doc. Extract these fields for me, no matter how they’re named.”
The LLM understands the semantic context. It acknowledges that “Order Reference” and “PO Quantity” imply the identical factor, even with out an express rule.
Execute Script 2 for Strategy 2
Now, we create a brand new file known as approach2_llm.py with the next code that you could find within the GitHub-Gist inside the identical folder:
https://gist.github.com/Sari95/d4e9e83490a9fbf34a3776d1604f8742
Now we execute the file once more contained in the terminal. Ensure that Ollama continues to be working within the background:
python approach2_llm.py
The Results of Strategy 2
What we will now see is that each layouts are accurately acknowledged:

For each layouts, the knowledge from the otherwise named fields is accurately extracted and assigned, though not a single regex expression was adjusted and no new template was created. The LLM understands each layouts as a result of it reads the context. Moreover, the date format from Format B is straight normalized to match the format from Format A.
2 – Head-to-Head Comparability
After each exams, one factor shortly turns into clear: Technically, each approaches remedy the identical drawback.
Each approaches have their very own benefits and downsides:

With regex-based pipelines, the complexity lives within the guidelines and upkeep effort. With LLM-based pipelines, the complexity shifts towards infrastructure, inference time, and mannequin habits. For medium-sized firms processing many customer-specific layouts, that trade-off can turn out to be strategically extra necessary than pure extraction accuracy.
3 – When ought to we NOT use an LLM?
For the time being, it typically feels as if each present automation course of abruptly must be changed with AI or LLMs.
In apply, nonetheless, this isn’t at all times the higher answer. Particularly medium-sized firms normally don’t must construct the “most fashionable” answer, however moderately the one that is still secure, maintainable, and economically cheap in the long run. Relying on the state of affairs, that may be the standard regex-based strategy, whereas in different instances switching to an LLM might make extra sense.
Some conditions the place the standard strategy should still be the extra appropriate possibility:
- The paperwork are secure and standardized:
If an organization solely processes just a few identified layouts and these hardly ever change, regex is usually the higher answer.Why?
As a result of the extra good thing about an LLM turns into small, whereas the general system complexity will increase.
A secure rule-based course of, alternatively, is quicker, cheaper, simpler to debug, and simpler handy over to new folks.
- Pace and throughput are vital:
In our instance, the LLM processes one doc inside 20–40 seconds.At first, that sounds acceptable. However as soon as we think about ourselves inside an actual manufacturing surroundings, the attitude adjustments shortly.
A medium-sized firm most likely processes orders, supply notes, invoices, customs paperwork, assist paperwork, and so forth. And never 10 occasions per day, however 10,000 occasions per day.
On this state of affairs, inference time abruptly turns into an actual infrastructure subject. Regex-based programs run considerably sooner, whereas LLMs require extra RAM, extra CPU/GPU energy, and infrequently further queueing or batch-processing mechanisms.
- Explainability is extra necessary than flexibility:
Particularly in regulated industries comparable to pharma, insurance coverage, banking, or healthcare, it’s typically obligatory to totally perceive why a selected worth was extracted.Regex guidelines are clearly deterministic: One line of code produces one clearly explainable outcome. LLMs, alternatively, work probabilistically: The mannequin interprets the context and returns the most definitely outcome. That is precisely what makes LLMs versatile, however on the identical time additionally tougher to audit.
- The corporate doesn’t have the fitting infrastructure:
In our instance, we used Ollama. Getting began was usually easy. However, it shouldn’t be underestimated that reminiscence consumption, GPU sources, monitoring, or response occasions underneath load can look very completely different when working with LLMs.
On my Substack Knowledge Science Espresso, I share sensible guides and bite-sized updates from the world of Knowledge Science, Python, AI, Machine Studying, and Tech — made for curious minds like yours.
Take a look and subscribe on Medium or on Substack if you wish to keep within the loop.
4 – Last Ideas
Selecting the best strategy just isn’t essentially a technical query, however moderately a strategic one.
The standard strategy tries to explicitly describe each attainable doc. The LLM-based strategy as an alternative tries to grasp that means and context. For small and secure environments, the standard strategy is usually fully adequate. The extra layouts and edge instances seem, the tougher it turns into to maintain the principles maintainable in the long run. That’s precisely the place LLMs begin to turn out to be attention-grabbing.
It can be an thrilling entry-level use case for a corporation to start out working with an LLM right here and, in doing so, make the corporate prepared for AI and achieve preliminary sensible expertise.

