docx-templates-2-loops-and-iterations
Sub-skill of docx-templates: 2. Loops and Iterations.
Best use case
docx-templates-2-loops-and-iterations is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Sub-skill of docx-templates: 2. Loops and Iterations.
Teams using docx-templates-2-loops-and-iterations should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/2-loops-and-iterations/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How docx-templates-2-loops-and-iterations Compares
| Feature / Agent | docx-templates-2-loops-and-iterations | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Sub-skill of docx-templates: 2. Loops and Iterations.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# 2. Loops and Iterations
## 2. Loops and Iterations
**Rendering Lists and Tables:**
```python
"""
Use loops to render lists, tables, and repeated content.
"""
from docxtpl import DocxTemplate
from typing import List, Dict, Any
from dataclasses import dataclass
from decimal import Decimal
@dataclass
class LineItem:
"""Invoice line item."""
description: str
quantity: int
unit_price: Decimal
discount: Decimal = Decimal("0")
@property
def subtotal(self) -> Decimal:
return self.quantity * self.unit_price * (1 - self.discount / 100)
@dataclass
class Invoice:
"""Invoice data structure."""
invoice_number: str
date: str
due_date: str
client_name: str
client_address: str
items: List[LineItem]
tax_rate: Decimal = Decimal("8.5")
notes: str = ""
@property
def subtotal(self) -> Decimal:
return sum(item.subtotal for item in self.items)
@property
def tax_amount(self) -> Decimal:
return self.subtotal * self.tax_rate / 100
@property
def total(self) -> Decimal:
return self.subtotal + self.tax_amount
def to_context(self) -> Dict[str, Any]:
"""Convert to template context."""
return {
"invoice_number": self.invoice_number,
"date": self.date,
"due_date": self.due_date,
"client_name": self.client_name,
"client_address": self.client_address,
"items": [
{
"description": item.description,
"quantity": item.quantity,
"unit_price": f"${item.unit_price:.2f}",
"discount": f"{item.discount}%" if item.discount else "",
"subtotal": f"${item.subtotal:.2f}"
}
for item in self.items
],
"subtotal": f"${self.subtotal:.2f}",
"tax_rate": f"{self.tax_rate}%",
"tax_amount": f"${self.tax_amount:.2f}",
"total": f"${self.total:.2f}",
"notes": self.notes
}
def render_invoice(
template_path: str,
output_path: str,
invoice: Invoice
) -> None:
"""
Render invoice template with line items.
Template syntax for loops:
{%tr for item in items %}
{{ item.description }} | {{ item.quantity }} | {{ item.unit_price }}
{%tr endfor %}
Note: {%tr %} is for table rows, {%p %} for paragraphs
"""
template = DocxTemplate(template_path)
context = invoice.to_context()
template.render(context)
template.save(output_path)
def render_list_document(
template_path: str,
output_path: str,
items: List[str],
title: str
) -> None:
"""
Render document with bullet list.
Template syntax for paragraph loops:
{%p for item in items %}
- {{ item }}
{%p endfor %}
"""
template = DocxTemplate(template_path)
context = {
"title": title,
"items": items,
"item_count": len(items)
}
template.render(context)
template.save(output_path)
# Example: Create invoice
invoice = Invoice(
invoice_number="INV-2026-0042",
date="January 17, 2026",
due_date="February 16, 2026",
client_name="Acme Corp",
client_address="123 Main St\nNew York, NY 10001",
items=[
LineItem("Consulting Services", 40, Decimal("150.00")),
LineItem("Software License", 1, Decimal("500.00")),
LineItem("Training Session", 8, Decimal("100.00"), Decimal("10")),
],
notes="Payment due within 30 days. Thank you for your business!"
)
# render_invoice("invoice_template.docx", "invoice_output.docx", invoice)
def render_nested_loops(
template_path: str,
output_path: str,
departments: List[Dict]
) -> None:
"""
Render template with nested loops.
Template syntax:
{%p for dept in departments %}
Department: {{ dept.name }}
{%p for emp in dept.employees %}
- {{ emp.name }} ({{ emp.role }})
{%p endfor %}
{%p endfor %}
"""
template = DocxTemplate(template_path)
context = {
"company_name": "TechCorp",
"departments": departments
}
template.render(context)
template.save(output_path)
# Example data for nested loops
departments = [
{
"name": "Engineering",
"head": "Alice Johnson",
"employees": [
{"name": "Bob Smith", "role": "Senior Developer"},
{"name": "Carol White", "role": "Developer"},
{"name": "David Brown", "role": "QA Engineer"}
]
},
{
"name": "Marketing",
"head": "Eve Davis",
"employees": [
{"name": "Frank Miller", "role": "Marketing Manager"},
{"name": "Grace Lee", "role": "Content Writer"}
]
}
*Content truncated — see parent skill for full reference.*Related Skills
docx
Comprehensive Word document toolkit for reading, creating, and editing .docx files. Supports text extraction, document creation with python-docx, and tracked changes via redlining workflow. Use for legal, academic, or professional document manipulation.
n8n-8-workflow-templates-and-subworkflows
Sub-skill of n8n: 8. Workflow Templates and Subworkflows.
github-swarm-pr-1-pr-templates-for-swarm
Sub-skill of github-swarm-pr: 1. PR Templates for Swarm.
github-swarm-issue-issue-templates-for-swarms
Sub-skill of github-swarm-issue: Issue Templates for Swarms.
github-swarm-issue-1-issue-templates
Sub-skill of github-swarm-issue: 1. Issue Templates (+3).
pandoc-5-custom-latex-templates
Sub-skill of pandoc: 5. Custom LaTeX Templates (+1).
plotly-built-in-templates
Sub-skill of plotly: Built-in Templates (+1).
python-docx-report-generation-from-database
Sub-skill of python-docx: Report Generation from Database (+1).
python-docx-batch-document-generation
Sub-skill of python-docx: Batch Document Generation.
python-docx-6-style-management-and-custom-styles
Sub-skill of python-docx: 6. Style Management and Custom Styles.
python-docx-5-image-insertion-and-positioning
Sub-skill of python-docx: 5. Image Insertion and Positioning.
python-docx-4-headers-footers-and-page-setup
Sub-skill of python-docx: 4. Headers, Footers, and Page Setup.