<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python Data Automation]]></title><description><![CDATA[Python Data Automation]]></description><link>https://pythondataautomation.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 02:20:04 GMT</lastBuildDate><atom:link href="https://pythondataautomation.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Extract Structured JSON from PDF Invoices in Python (Without Messy Regex)]]></title><description><![CDATA[Practical Python tutorials for automating backend data workflows—extracting JSON from PDFs, cleaning email leads, and normalizing e-commerce CSVs.]]></description><link>https://pythondataautomation.hashnode.dev/how-to-extract-structured-json-from-pdf-invoices-in-python-without-messy-regex</link><guid isPermaLink="true">https://pythondataautomation.hashnode.dev/how-to-extract-structured-json-from-pdf-invoices-in-python-without-messy-regex</guid><category><![CDATA[Python]]></category><category><![CDATA[api]]></category><category><![CDATA[automation]]></category><category><![CDATA[Backend Development]]></category><category><![CDATA[json]]></category><dc:creator><![CDATA[medhakamath]]></dc:creator><pubDate>Thu, 26 Nov 2026 16:40:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6b06eb46a9c15cf185f11776/ed121710-cb98-4501-839f-8f8fd7f57116.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you’ve ever built an automated invoice or receipt pipeline in Python, you know that PDFs are where clean code goes to die. Every vendor formats their documents differently. One puts "Total Due:" at the bottom right, another uses "Amount Payable" inside a nested table, and a third puts the invoice date in the header margin.</p>
<p>###The Trap: Brittle pypdf + Regex Scripts</p>
<p>Most Python pipelines start with a quick text-extraction script that looks simple at first:</p>
<pre><code class="language-python">import re
from pypdf import PdfReader

text = PdfReader("invoice.pdf").pages[0].extract_text()
# Works on Vendor A, silently breaks on Vendor B
total = re.search(r"Total Due:\s*\$([\d,]+\.\d{2})", text)
</code></pre>
<p>It works great until a supplier redesigns their template, switches currency symbols (€ or £), or splits line items across two pages—leaving you maintaining dozens of fragile, vendor-specific regex rules. Instead of hardcoding layout rules for every supplier, you can extract normalized JSON (including vendor details, tax breakdowns, and line items) in a single request using the PDF Invoice &amp; Receipt Data Extractor API.</p>
<h2>What We Will Extract</h2>
<p>The API normalizes key fields such as vendor details (<code>vendor_name</code>, <code>vendor_email</code>), invoice metadata (<code>invoice_number</code>, <code>invoice_date</code>, <code>currency</code>), and financial amounts (<code>subtotal_amount</code>, <code>tax_amount</code>, <code>total_amount</code>).</p>
<h2>Prerequisites</h2>
<ol>
<li><strong>Python 3.7+</strong></li>
<li>The <code>requests</code> library (<code>pip install requests</code>)</li>
<li>A free API key from your <a href="https://rapidapi.com/medhakamath/api/pdf-invoice-receipt-data-extractor">PDF Invoice &amp; Receipt Data Extractor API listing</a> (Basic tier includes 50 free requests/month).</li>
</ol>
<hr />
<h2>Core Extraction Methods</h2>
<h3>1. Local PDF File (<code>/v1/parse-pdf-file</code>)</h3>
<p>Use <code>multipart/form-data</code> to upload a local PDF file:</p>
<pre><code class="language-python">import requests

url = "https://pdf-invoice-receipt-data-extractor.p.rapidapi.com/v1/parse-pdf-file"
headers = {
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

with open("invoice.pdf", "rb") as pdf_file:
    response = requests.post(
        url,
        headers=headers,
        files={"file": ("invoice.pdf", pdf_file, "application/pdf")}
    )

if response.status_code == 200:
    print(response.json())
</code></pre>
<h3>2. Public PDF URL (<code>/v1/parse-pdf-url</code>)</h3>
<p>Pass a direct HTTPS link to a hosted PDF without downloading it first:</p>
<pre><code class="language-python">import requests

url = "https://pdf-invoice-receipt-data-extractor.p.rapidapi.com/v1/parse-pdf-url"
payload = {"url": "https://example.com/sample_invoice.pdf"}
headers = {
    "content-type": "application/json",
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
</code></pre>
<h3>3. Raw Invoice Text (<code>/v1/parse-invoice-text</code>)</h3>
<p>Send extracted text strings directly from emails or OCR pipelines:</p>
<pre><code class="language-python">import requests

url = "https://pdf-invoice-receipt-data-extractor.p.rapidapi.com/v1/parse-invoice-text"
payload = {"text": "ACME Cloud Services Inc.\nInvoice #INV-2026-8841\nTotal Due: \$295.00 USD"}
headers = {
    "content-type": "application/json",
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
</code></pre>
<hr />
<p>###Example Output</p>
<p>{ "status": "success", "data": { "vendor_name": "ACME Cloud Services Inc.", "vendor_email": "<a href="mailto:billing@acmecloud.io">billing@acmecloud.io</a>", "invoice_number": "INV-2026-8841", "invoice_date": "2026-11-18", "currency": "USD", "subtotal_amount": 295.00, "tax_amount": 0.00, "total_amount": 295.00, "line_items": [ { "description": "Cloud Infrastructure Hosting - Nov 2026", "quantity": 1, "unit_price": 295.00, "amount": 295.00 } ] } }</p>
<h3>3 PDF Invoice Gotchas to Watch Out For in Production</h3>
<p>• Scanned vs. Digital PDFs: If a vendor emails a flat scanned image saved as a .pdf, standard text extractors will return an empty string—always run scanned files through OCR first and pass the output to /v1/parse-invoice-text. • Line Item Math Validation: Even with clean extraction, always run a quick sanity check in Python (sum(item["amount"]) + tax_amount == total_amount) before pushing financial records into your accounting database. • Multi-Currency Formatting: European invoices often swap commas and decimals (1.250,00 vs 1,250.00)—relying on a normalized currency field and typed float outputs prevents silent accounting errors.</p>
<h2>Resources &amp; Next steps</h2>
<ul>
<li>You can find the documentation in the <a href="https://github.com/medhakamath/pdf-invoice-receipt-data-extractor-api">GitHub Repository</a>.</li>
<li>Test the live endpoints directly on the <a href="https://rapidapi.com/medhakamath/api/pdf-invoice-receipt-data-extractor">PDF Invoice &amp; Receipt Data Extractor RapidAPI page</a></li>
</ul>
<p>How are you currently handling multi-vendor PDF invoices in Python—custom regex templates, OCR pipelines, or LLM extraction? Share your stack and worst PDF parsing horror story in the comments!</p>
]]></content:encoded></item></channel></rss>