54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""Create a sample .docx template with Jinja2 variables for testing."""
|
|
|
|
from docx import Document
|
|
from docx.shared import Pt
|
|
from pathlib import Path
|
|
|
|
|
|
def set_cell_text(cell, text: str) -> None:
|
|
cell.text = ""
|
|
cell.paragraphs[0].add_run(text)
|
|
|
|
|
|
output = Path(__file__).parent / "invoice_template.docx"
|
|
|
|
doc = Document()
|
|
|
|
title = doc.add_paragraph()
|
|
run = title.add_run("Invoice Document")
|
|
run.bold = True
|
|
run.font.size = Pt(18)
|
|
|
|
doc.add_paragraph("Client: {{ client_name }}")
|
|
doc.add_paragraph("Date: {{ invoice_date }}")
|
|
doc.add_paragraph("Invoice #: {{ invoice_number }}")
|
|
|
|
doc.add_paragraph()
|
|
doc.add_paragraph("Description:")
|
|
doc.add_paragraph("{{ description }}")
|
|
|
|
doc.add_paragraph()
|
|
doc.add_paragraph("Items:")
|
|
|
|
table = doc.add_table(rows=2, cols=3)
|
|
table.style = "Table Grid"
|
|
|
|
hdr = table.rows[0].cells
|
|
set_cell_text(hdr[0], "Product")
|
|
set_cell_text(hdr[1], "Quantity")
|
|
set_cell_text(hdr[2], "Price")
|
|
|
|
data_row = table.rows[1].cells
|
|
set_cell_text(data_row[0], "{%tr for item in items %}{{ item.product }}")
|
|
set_cell_text(data_row[1], "{{ item.qty }}")
|
|
set_cell_text(data_row[2], "{{ item.price }}{%tr endfor %}")
|
|
|
|
doc.add_paragraph()
|
|
doc.add_paragraph("Total: {{ total_amount }}")
|
|
|
|
doc.add_paragraph()
|
|
doc.add_paragraph("Prepared by: {{ author_name }}")
|
|
|
|
doc.save(str(output))
|
|
print(f"Created: {output}")
|