Categories We Write About

Generate invoice PDFs from spreadsheet data

To generate invoice PDFs from spreadsheet data, you can use tools or write a script to automate the process. Here’s a step-by-step approach using Python with pandas, reportlab, and openpyxl or xlrd:


Step 1: Prepare Your Spreadsheet

Your Excel or CSV file should include data like:

Invoice NoDateClient NameItem DescriptionQuantityUnit PriceTotal
INV0012025-05-15John SmithWidget A32060

Each row can represent either:

  • A full invoice (simple case), or

  • An invoice item, grouped by Invoice No (if one invoice has multiple items).


Step 2: Install Required Python Libraries

bash
pip install pandas reportlab openpyxl

Step 3: Python Script to Generate PDFs

python
import pandas as pd from reportlab.lib.pagesizes import A4 from reportlab.pdfgen import canvas import os # Load spreadsheet data df = pd.read_excel('invoices.xlsx') # or pd.read_csv('invoices.csv') # Group by Invoice No if multiple items per invoice grouped = df.groupby('Invoice No') # Output directory output_dir = 'invoices' os.makedirs(output_dir, exist_ok=True) # Generate PDF for each invoice for invoice_no, data in grouped: c = canvas.Canvas(f"{output_dir}/{invoice_no}.pdf", pagesize=A4) width, height = A4 c.setFont("Helvetica-Bold", 16) c.drawString(50, height - 50, f"Invoice: {invoice_no}") c.setFont("Helvetica", 12) c.drawString(50, height - 80, f"Date: {data['Date'].iloc[0]}") c.drawString(50, height - 100, f"Client: {data['Client Name'].iloc[0]}") # Table header c.setFont("Helvetica-Bold", 12) c.drawString(50, height - 140, "Description") c.drawString(250, height - 140, "Quantity") c.drawString(350, height - 140, "Unit Price") c.drawString(450, height - 140, "Total") y = height - 160 c.setFont("Helvetica", 12) for index, row in data.iterrows(): c.drawString(50, y, str(row['Item Description'])) c.drawString(250, y, str(row['Quantity'])) c.drawString(350, y, f"${row['Unit Price']:.2f}") c.drawString(450, y, f"${row['Total']:.2f}") y -= 20 # Total amount total_sum = data['Total'].sum() c.setFont("Helvetica-Bold", 12) c.drawString(50, y - 20, f"Grand Total: ${total_sum:.2f}") c.save()

Step 4: Run the Script

  1. Save your spreadsheet as invoices.xlsx.

  2. Save the script as generate_invoices.py.

  3. Run the script:

bash
python generate_invoices.py

Each PDF will be saved in the invoices folder.


This method is ideal for generating bulk invoices from structured data. Let me know if you’d like a version that includes logos, tax, or itemized tax breakdowns.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About