DEV Community

Cover image for A Small Python Script to Check Your Invoice Data Before an E-Invoicing Rollout
Perfonec Computers
Perfonec Computers

Posted on

A Small Python Script to Check Your Invoice Data Before an E-Invoicing Rollout

When a country introduces mandatory e-invoicing, the software choice gets most of the attention. In practice, a lot of failures come from something duller: messy master data. Missing tax numbers, free-text addresses and vague product names are easy to miss in daily work but cause trouble once invoices must be structured and validated.

This post shows a small Python script that scans two CSV exports, one of customers and one of items, and lists the problems it finds. It uses only the standard library, so there is nothing to install.

What it checks

For customers: a tax registration number (TRN) that is 15 digits long, and non-empty street, city and country fields. For items: a description that is not too short or generic, and a non-empty unit of measure.

These checks are deliberately simple. Your country’s rules may require more, so treat this as a first pass, not a validator.

The script
python
import csv, re, sys

GENERIC = {"item", "items", "goods", "service", "services",
"misc", "other", "product", "general"}

def check_customers(path):
problems = []
with open(path, newline="", encoding="utf-8") as f:
for n, row in enumerate(csv.DictReader(f), start=2):
name = row.get("name", "").strip() or f"row {n}"
trn = re.sub(r"\D", "", row.get("trn", ""))
if len(trn) != 15:
problems.append((name, "TRN is missing or not 15 digits"))
for field in ("street", "city", "country"):
if not row.get(field, "").strip():
problems.append((name, f"{field} is empty"))
return problems

def check_items(path):
problems = []
with open(path, newline="", encoding="utf-8") as f:
for n, row in enumerate(csv.DictReader(f), start=2):
desc = row.get("description", "").strip()
if len(desc) < 5 or desc.lower() in GENERIC:
problems.append((f"row {n}", f"description too generic: '{desc}'"))
if not row.get("unit", "").strip():
problems.append((f"row {n}", "unit of measure is empty"))
return problems

if name == "main":
kind, path = sys.argv[1], sys.argv[2]
found = check_customers(path) if kind == "customers" else check_items(path)
for who, msg in found:
print(f"{who}: {msg}")
print(f"\n{len(found)} problem(s) found")
Try it

Create customers.csv:

csv
name,trn,street,city,country
Acme Trading,100123456789012,Street 1,Dubai,AE
Old Client,12345,,Sharjah,AE

Create items.csv:

csv
description,unit
Brake pad set,EA
Services,
Goods,KGM

Run:

bash
python check_invoice_data.py customers customers.csv
python check_invoice_data.py items items.csv

On these samples you get:

Old Client: TRN is missing or not 15 digits
Old Client: street is empty

2 problem(s) found

and

row 3: description too generic: 'Services'
row 3: unit of measure is empty
row 4: description too generic: 'Goods'

3 problem(s) found
Ideas to extend it

You could add a column check for country codes, flag duplicate TRNs, or write results to a CSV so a finance team can fix rows directly. If your accounting tool exports to Excel, openpyxl can read it instead of csv. A simple check like this will not replace official validation, but it catches the boring errors early, when they are cheap to fix.

Column names in your export will differ, so change the keys (trn, street, description and so on) to match your file.

Disclosure: I used AI assistance to draft this post and tested the script on the sample files above before publishing.

Top comments (0)