state of affairs: You’re employed within the operations crew of a medium-sized firm. On daily basis, your crew processes order types from completely different B2B prospects. All of them arrive as PDFs. And in principle, all of them include the identical info: buyer ID, buy order quantity, supply date, and the ordered objects.
In follow, nevertheless, each doc appears to be like 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 utterly 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 info is supposed.
For conventional automation techniques, nevertheless, this turns into tough: 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 examine two completely different approaches for extracting structured knowledge from B2B order types:
- A standard rule-based strategy utilizing pytesseract and regex guidelines
- An LLM-based strategy utilizing pytesseract, Ollama, and LLaMA 3
The objective of this text is to not present that LLMs are usually higher. They don’t seem to be 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 improve? 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 – Remaining 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 info however utilizing completely different layouts. Afterwards, we extract the information as soon as with a standard OCR and regex pipeline and as soon as with an OCR and LLM pipeline. This enables us to match each approaches underneath equivalent circumstances.
- The normal strategy principally asks:
“Can I discover the precise sample that I programmed?” - The LLM-based strategy as an alternative asks:
“Can I perceive the which means of this subject 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 customary 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 robotically whenever you set up Python. If you understand Python tutorials that work with Anaconda, that’s merely one other technique to obtain the identical objective (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 machine we use the CMD terminal (Home windows key + R > click on on cmd).
Create and activate a brand new digital atmosphere
Create a brand new python atmosphere with python –m venv b2bdocumentextractor (you’ll be able to change the identify) in a terminal and activate it withb2bdocumentextractorScriptsactivate.
Elective: Examine Python and pip
python --version
pip --version
It is best to see a Python and a pip model.
Step 1 – Set up Tesseract
Tesseract is the OCR engine. It’s the software that really reads textual content from photos or scanned PDFs utilizing OCR (Optical Character Recognition). pytesseract is barely 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 not work.
First, we obtain the most recent .exe-file for w64 and run the installer:
GitHub – Tesseract at UB Mannheim
Essential: 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 every little thing labored accurately, we must always see the corresponding Tesseract model.

Step 2 – Set up Poppler
Subsequent, we set up pdf2image. That is our library for changing PDFs into photos 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 appears to be like 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 situated.
Trace for Newbies:
Press the Home windows key and seek for Edit atmosphere variables. Afterwards click on on Edit the system atmosphere variables. Then click on on Surroundings Variables. Underneath 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. Be sure to reactivate the Python atmosphere 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 implies it acknowledges textual content from pixels in a picture. It can not learn PDF constructions straight. pdf2image subsequently performs an intermediate step: It renders every PDF web page as a picture, much like a screenshot, in order that pytesseract can analyze it afterwards. Word: If we had digital PDFs (which means PDFs the place you’ll be able to choose and duplicate textual content), we may straight extract the textual content utilizing libraries akin to pdfplumber or PyMuPDF. Nevertheless, since we assume that B2B order types are sometimes scans in follow, 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 photos.
fpdf2: We use this library to robotically generate two check PDFs (Structure A and Structure 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 software that enables us to run LLMs utterly free, regionally on our laptop computer, and with out API keys.
First, we set up Ollama. In case you have not already achieved this, you’ll be able to 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 are able to see a progress bar within the terminal.

Afterwards, we confirm whether or not every little thing labored:
ollama listing
In the event you see one thing much like the screenshot, it labored efficiently.

Step 5 – Create the Mission Folder and Generate Take a look at PDFs
For this comparability, we create two B2B order types for Alpha GmbH and Beta AG that include the identical info however use completely different layouts. On this instance, we assume that the order types are scans, which is why we beforehand put in pdf2image (for digital PDFs, this is able to even be doable with libraries akin 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 are able to now see the 2 newly created PDFs:

Within the two PDFs, we are able to already see the issue:
- They include the identical info.
- However the PDFs use utterly completely different subject names and a distinct date format.
Strategy 1: The Conventional Manner (pytesseract + Regex Guidelines)
The normal 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 software “appears to be like” on the picture and tries to acknowledge letters from pixels. Fairly much 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 are able to outline: “Seek for every little thing that comes after
PO Quantity:.”
Already on this second step, we are able to determine the primary downside: What occurs if the client merely writes “Order Reference” as an alternative of “PO Quantity: “?
In that case, the regex sample finds nothing. What we are able to 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 Structure A, every little thing works completely:
For Structure B? Not a single subject 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 hold the OCR step, however change 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. Guarantee that Ollama continues to be working within the background:
python approach2_llm.py
The Results of Strategy 2
What we are able to now see is that each layouts are accurately acknowledged:

For each layouts, the knowledge from the in a different way 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 Structure B is straight normalized to match the format from Structure A.
2 – Head-to-Head Comparability
After each assessments, one factor shortly turns into clear: Technically, each approaches remedy the identical downside.
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 corporations processing many customer-specific layouts, that trade-off can develop into strategically extra necessary than pure extraction accuracy.
3 – When ought to we NOT use an LLM?
In the meanwhile, it typically feels as if each current automation course of all of a sudden must be changed with AI or LLMs.
In follow, nevertheless, this isn’t at all times the higher resolution. Particularly medium-sized corporations normally don’t must construct the “most fashionable” resolution, however fairly the one that is still steady, maintainable, and economically cheap in the long run. Relying on the state of affairs, that may be the normal regex-based strategy, whereas in different instances switching to an LLM might make extra sense.
Some conditions the place the normal strategy should still be the extra appropriate choice:
- The paperwork are steady and standardized:
If an organization solely processes a number of recognized layouts and these hardly ever change, regex is commonly the higher resolution.Why?
As a result of the extra advantage of an LLM turns into small, whereas the general system complexity will increase.
A steady rule-based course of, however, is quicker, cheaper, simpler to debug, and simpler handy over to new folks.
- Pace and throughput are important:
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 atmosphere, the attitude adjustments shortly.
A medium-sized firm most likely processes orders, supply notes, invoices, customs paperwork, assist paperwork, and so on. And never 10 instances per day, however 10,000 instances per day.
On this state of affairs, inference time all of a sudden turns into an actual infrastructure difficulty. Regex-based techniques run considerably quicker, whereas LLMs require extra RAM, extra CPU/GPU energy, and infrequently extra queueing or batch-processing mechanisms.
- Explainability is extra necessary than flexibility:
Particularly in regulated industries akin to pharma, insurance coverage, banking, or healthcare, it’s typically obligatory to completely perceive why a particular worth was extracted.Regex guidelines are clearly deterministic: One line of code produces one clearly explainable consequence. LLMs, however, work probabilistically: The mannequin interprets the context and returns the most probably consequence. That is precisely what makes LLMs versatile, however on the similar time additionally harder to audit.
- The corporate doesn’t have the precise 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 instances 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 – Remaining Ideas
Choosing the proper strategy isn’t essentially a technical query, however fairly a strategic one.
The normal strategy tries to explicitly describe each doable doc. The LLM-based strategy as an alternative tries to grasp which means and context. For small and steady environments, the normal strategy is commonly utterly enough. The extra layouts and edge instances seem, the harder it turns into to maintain the principles maintainable in the long run. That’s precisely the place LLMs begin to develop into attention-grabbing.
It can be an thrilling entry-level use case for an organization to start out working with an LLM right here and, in doing so, make the corporate prepared for AI and achieve preliminary sensible expertise.
















