HomeCourseModule 12 › Filling a template workbook

Filling a template workbook

Module 12 · Automating Boring Excel Tasks8 min readIntermediate

What you'll learn

  • Load an Excel template with openpyxl
  • Write values into named cells
  • Save the filled copy with a unique name

Why templates beat from-scratch reports

Excel templates can have logos, frozen panes, formulas, colours, named ranges — all the spreadsheety polish. With openpyxl you can open the template, plug in values, and save a copy — keeping every bit of formatting.

The script

from openpyxl import load_workbook
from datetime import date
import pandas as pd

template = "templates/invoice.xlsx"
orders = pd.read_csv("orders_to_invoice.csv")

for _, row in orders.iterrows():
    wb = load_workbook(template)
    ws = wb.active

    ws["B2"] = f"Invoice {row['invoice_no']}"
    ws["B3"] = row["customer"]
    ws["B4"] = row["billing_address"]
    ws["B5"] = date.today().isoformat()
    ws["E10"] = row["quantity"]
    ws["E11"] = row["unit_price"]
    ws["E13"] = row["tax_rate"]
    # formulas in the template will recalc when the file is opened

    out = f"invoices/{row['invoice_no']}.xlsx"
    wb.save(out)
    print(f"  wrote {out}")

Use named ranges to make this readable

In Excel: Formulas → Define Name. Then in Python:

wb["TemplateSheet"]["CustomerName"]   # if you defined CustomerName as a named range

(For most everyday cases, the ws["B3"] style is fine.)

Key takeaways

  • Templates keep formatting; openpyxl just stamps data into specific cells.
  • Loop a DataFrame, one template-copy per row.
  • Formulas in the template still work — they recalc on open.

Receipt printer

Build a tiny Excel "receipt" template with a logo and a totals formula. Loop a small DataFrame and generate one receipt per row.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.