Data Automation & Cleaning
Intermediate
Extract Line Items, GSTIN & Invoice Totals from PDF Invoices using Regex in Python
Parse unstructured vendor PDF invoices into structured Pandas DataFrames using pdfplumber and regular expressions in Python.
#pdfplumber#pandas
Problem Statement
Accounts payable teams process thousands of unstructured PDF vendor invoices manually. This script extracts invoice numbers, dates, GSTINs, and totals automatically.
Formula / Core Logic
Extracts text via pdfplumber and applies compiled regex patterns for Invoice Number, Grand Total, and Tax amounts.
Installation
pip install pdfplumber pandas
pdf-invoice-data-extraction-regex-python.py
import pdfplumber
import re
import pandas as pd
.def parse_pdf_invoice(pdf_path):
text = ''
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
text += page.extract_text() or ''
inv_match = re.search(r'Invoice\s*No[:\s]*([A-Za-z0-9/-]+)', text, re.IGNORECASE)
tot_match = re.search(r'Total[:\s]*([0-9,.]+)', text, re.IGNORECASE)
return {
'Invoice_No': inv_match.group(1) if inv_match else 'UNKNOWN',
'Total_Amount': tot_match.group(1) if tot_match else '0.00'
}
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: Does this work on scanned image PDFs?
Scanned PDFs require OCR (pytesseract); pdfplumber works best on digital native PDF invoices.