How I Built a Local CSV-to-PDF Bulk Invoice Generator in Python with fpdf2
Generating client invoices one by one from a spreadsheet takes way more time than it should—and when you're rushing through billing rows, small formatting mistakes slip in easily. To solve that for small billing workflo
Generating client invoices one by one from a spreadsheet takes way more time than it should—and when you're rushing through billing rows, small formatting mistakes slip in easily.
To solve that for small billing workflows without paying for another recurring SaaS tool, I built a lightweight local script in Python using fpdf2 and a config.json file that turns batch CSV orders into clean, styled PDF invoices in a single run.
Why Local + Config-Driven Works Well
When building small business utilities, keeping configuration outside the core script makes maintenance much easier:
-
CSV input (
sample_orders.csv): Stores order/client rows cleanly so batch runs are repeatable. -
config.json: Holds company details, currency formatting, tax percentages, accent colors, and footer notes—so you can tweak branding or tax rates without editing Python logic. -
fpdf2: Generates crisp, printable PDFs locally with zero external cloud dependencies or monthly subscriptions.
Core Flow: Reading CSV & Calculating Totals
A typical invoice batch groups line items by invoice_id, calculates the subtotal, applies tax, and computes the grand total before rendering the PDF:
import csv
import json
from collections import defaultdict
from fpdf import FPDF
def load_config(path: str = "config.json") -> dict:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def group_orders_by_invoice(csv_path: str) -> dict:
invoices = defaultdict(list)
with open(csv_path, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
invoices[row["invoice_id"]].append(row)
return invoices
- GitHub Repository (code & sample data): https://github.com/josephttdeveloper/malabar-code-invoice-generator
- Ready-to-use package on Polar: https://buy.polar.sh/polar_cl_DQDILomjXrAT9xIEXBwDBnLEpt4rW1raDnb6136DaT1
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.