# `merge_office_template` — Merge .docx or .pptx Office Template

> Part of [Office OOXML Template Merge & Markdown Extractor API](https://office-template.apikeg.com/llms.txt) (`com.apikeg/office-template`)

Coalesces split {{variable}} runs across Word (<w:r>) and PowerPoint (<a:r>) paragraphs while preserving opening-run typography, expands {{#loop}}...{{/loop}} table rows, replaces variables across body/headers/footers/slides, and returns a valid PKZIP Base64 document plus any unmatchedPlaceholders.

**Why call instead of DIY:** Eliminates broken {{tags}} caused by Word/PowerPoint spellcheck run-splitting, <a:br/> vs <w:br/> schema errors, and corrupt PKZIP Central Directory CRCs.

- **REST Endpoint:** `POST https://office-template.apikeg.com/v1/merge-office-template`
- **MCP Server:** `https://office-template.apikeg.com/mcp` (tool: `merge_office_template`)
- **Pricing:** $0.004 USD per Office template merge (.docx or .pptx) (x402 instant micropayment)
- **Free Dry-Run:** `POST https://office-template.apikeg.com/v1/merge-office-template?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "fileBase64": {
      "type": "string",
      "minLength": 16,
      "description": "Base64-encoded .docx or .pptx OOXML template file (accepts raw Base64 or data:...;base64, URI)."
    },
    "format": {
      "type": "string",
      "enum": [
        "auto",
        "docx",
        "pptx"
      ],
      "default": "auto",
      "description": "Template format ('auto' inspects internal OOXML ZIP entries)."
    },
    "variables": {
      "type": "object",
      "additionalProperties": {
        "description": "Scalar replacement value (string, finite number, or boolean)."
      },
      "description": "Key-value map for {{placeholder}} variables across body, headers, footers, and slides. Multi-line '\\n' values expand to <w:br/> / <a:br/>."
    },
    "tableLoops": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "object",
          "additionalProperties": {
            "description": "Scalar replacement value (string, finite number, or boolean)."
          },
          "description": "Per-row variable map merged into the cloned table row."
        },
        "maxItems": 2000,
        "description": "Rows cloned for {{#loopKey}}...{{/loopKey}} inside <w:tr> or <a:tr>."
      },
      "description": "Repeating table row arrays keyed by loop name (matching {{#items}}...{{/items}} inside a Word or PowerPoint table row)."
    }
  },
  "required": [
    "fileBase64"
  ],
  "additionalProperties": false
}
```

## Examples
### Populate contract & line-item table in .docx template
```bash
curl -X POST "https://office-template.apikeg.com/v1/merge-office-template" \
  -H "Content-Type: application/json" \
  -d '{"fileBase64":"UEsDBBQAAAAIA...","format":"docx","variables":{"customer_name":"Acme Robotics, Inc.","billing_address":"500 Market St\nSuite 1200\nSan Francisco, CA","contract_id":"MSA-2026-089"},"tableLoops":{"items":[{"sku":"ENT-SEAT","description":"Enterprise Agent Seat","qty":25,"total":"$2,500"},{"sku":"DED-EDGE","description":"Dedicated Edge Worker","qty":1,"total":"$500"}]}}'
```
Expected output: Returns merged .docx Base64 bytes, replacementsCount=11, and unmatchedPlaceholders=[] ready to open in Microsoft Word.
