# Can OfficeCLI Generate Documents from Custom Templates? A Complete Guide

> Yes OfficeCLI generates documents from custom templates using the merge command. Replace {{placeholder}} tags in Word Excel and PowerPoint files with JSON data.

- Repository: [OfficeAI/OfficeCLI](https://github.com/iofficeai/OfficeCLI)
- Tags: how-to-guide
- Published: 2026-07-25

---

**OfficeCLI supports custom template generation through its `merge` command, which replaces `{{placeholder}}` tags in Word, Excel, and PowerPoint files with JSON data values.**

The iOfficeAI/OfficeCLI repository provides a command-line interface for automating Microsoft Office document workflows. Its template generation capability allows you to create dynamic documents from custom templates without manual editing. This feature is implemented by the `TemplateMerger` class and exposed through the `merge` command, enabling AI agents and automation scripts to produce populated Office files at scale.

## How Template Merging Works in OfficeCLI

### The TemplateMerger Class Implementation

The core document generation logic resides in [`src/officecli/Core/TemplateMerger.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/Core/TemplateMerger.cs). This class parses the OOXML structure of Office files and scans for text nodes matching the `{{key}}` placeholder pattern. It performs context-aware replacements throughout the document, including paragraph text, table cells, shape text, chart titles, headers, and footers, while preserving the original styling and layout.

### Supported File Formats and Placeholder Syntax

OfficeCLI generates documents from custom templates across three major Office formats:

- Word documents (`.docx`)
- Excel workbooks (`.xlsx`)
- PowerPoint presentations (`.pptx`)

Placeholders use the standard `{{variableName}}` syntax. When the `merge` command processes a template, it substitutes these placeholders with corresponding values from the provided JSON data payload.

## Generating Documents from Custom Templates: Practical Examples

### Single Document Generation with Inline JSON

To generate one document from a custom template, provide the JSON data directly via the `--data` flag:

```bash
officecli merge invoice-template.docx out-001.docx \
  --data '{"client":"Acme Corp","total":"$5,200","date":"2024-07-25"}'

```

This command creates `out-001.docx` by replacing every instance of `{{client}}`, `{{total}}`, and `{{date}}` in the template with the specified values.

### Batch Processing from JSON Files

For generating multiple documents, create a JSON array file and execute:

```bash
officecli merge invoice-template.docx out-$(seq -f "%03g" 1 10).docx \
  --data data.json

```

When [`data.json`](https://github.com/iOfficeAI/OfficeCLI/blob/main/data.json) contains an array of objects, OfficeCLI iterates through each entry and produces a separate output file for every data set, using the same custom template for all iterations.

### PowerPoint Template Processing

The `merge` command handles PowerPoint files identically to Word documents:

```bash
officecli merge q4-template.pptx q4-acme.pptx \
  --data '{"company":"Acme","quarter":"Q4","revenue":"$12M"}'

```

All `{{company}}`, `{{quarter}}`, and `{{revenue}}` placeholders on slides, shapes, and chart titles are populated with the JSON values.

### Excel Workbook Generation

Excel templates work with the same syntax:

```bash
officecli merge budget-template.xlsx budget-2024.xlsx \
  --data '{"dept":"Engineering","budget":"$1.5M"}'

```

Placeholders in cells, table headers, and chart titles are substituted while maintaining the workbook's formulas and formatting.

## CLI Integration and Command Structure

The `merge` command implementation in [`src/officecli/CommandBuilder.Merge.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/CommandBuilder.Merge.cs) provides the interface between the CLI and the `TemplateMerger` engine. According to the repository's [`SKILL.md`](https://github.com/iOfficeAI/OfficeCLI/blob/main/SKILL.md) file, this command is optimized for AI agent integration, allowing automated document generation pipelines that can process custom templates with minimal token costs and zero manual intervention.

## Summary

- OfficeCLI generates documents from custom templates via the `merge` command backed by the `TemplateMerger` class in [`src/officecli/Core/TemplateMerger.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/Core/TemplateMerger.cs).
- The tool supports Word (`.docx`), Excel (`.xlsx`), and PowerPoint (`.pptx`) files using `{{key}}` placeholder syntax.
- Data injection accepts JSON strings or JSON files via the `--data` parameter.
- Batch generation processes JSON arrays to create multiple documents from a single template.
- Template merging preserves all original formatting, styles, and document structure during placeholder replacement.

## Frequently Asked Questions

### Can OfficeCLI use any .docx file as a template?

Yes, any valid `.docx`, `.xlsx`, or `.pptx` file can serve as a custom template provided it contains `{{placeholder}}` tags where data should be injected. The `TemplateMerger` class parses the OOXML structure regardless of the document's original complexity or design.

### What JSON format does the merge command expect?

The `--data` flag accepts either a JSON object for single-document generation or a JSON array for batch processing. Each key must correspond to a placeholder name in the template minus the curly braces. For example, a placeholder `{{clientName}}` maps to a JSON key named `clientName`.

### Does template merging preserve formatting and styles?

Yes, the implementation in [`TemplateMerger.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/TemplateMerger.cs) performs context-aware replacement that maintains all original styling, fonts, colors, and layout properties. Only the text content of the placeholders is modified, ensuring the generated document retains the visual design of the custom template.

### Can I generate multiple documents from a single template?

Yes, by passing a JSON array to the `--data` parameter, you can generate multiple output files from one template file in a single command execution. This is efficient for producing bulk reports, invoices, or personalized presentations using the same custom template design.