Invoice Data Extractor with OCR

Solution for the use case Simple Invoice Data Extractor.

AI tools
Python, Tesseract OCR, OpenCV, Regex, PDF Processing, Excel Export
Language
Python
Accuracy
91%
Published
2025-06-26

Approach

Automatically extracts key information from invoices and receipts using OCR technology, perfect for expense tracking and accounting.

Implementation steps

  1. Install OCR libraries
  2. Preprocess images for better recognition
  3. Extract text using Tesseract
  4. Parse structured data with regex
  5. Validate extracted information
  6. Export to spreadsheet formats

Code (Python)

import cv2
import pytesseract
import re
import pandas as pd
from PIL import Image

class InvoiceDataExtractor:
    def __init__(self):
        self.amount_pattern = r"\$?(\d+[\.,]\d{2})"
        self.date_pattern = r"(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4})"
        self.invoice_pattern = r"(INV|Invoice)[#\s]*:?\s*(\w+\d+)"
        
    def extract_text(self, image_path):
        img = cv2.imread(image_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        text = pytesseract.image_to_string(gray)
        return text
        
    def parse_invoice_data(self, text):
        data = {}
        
        invoice_match = re.search(self.invoice_pattern, text, re.IGNORECASE)
        if invoice_match:
            data["invoice_number"] = invoice_match.group(2)
            
        date_match = re.search(self.date_pattern, text)
        if date_match:
            data["date"] = date_match.group(1)
            
        amounts = re.findall(self.amount_pattern, text)
        if amounts:
            data["total_amount"] = amounts[-1]
                
        return data

Prompts

Extract data from this invoice: Company name, invoice number, date, total amount, tax amount, and line items. Format the output as structured data.

Performance

Text Recognition: 91% accuracy. Processing Speed: 3-5 seconds per document. Format Support: PDF, JPG, PNG. Data Validation: Built-in.

Lessons learned

Drag-and-drop interface for easy uploads. Real-time preview of extracted data. Bulk processing for multiple invoices. Export to Excel/CSV.

Back to the use case · All AI use cases