Skip to content

Executable

Every library release comes with a precompiled executable that can be used directly from the command line, without the need to write any Go code.

Download

Download the executable for your operating system from the latest release.
Here's a source code.

Usage

Usage: template_docx_os_arch.exe <file.docx> <values.json> [<values2.json> ...]

The output files will have the filename of the JSON file they are generated from (e.g., values.docx, values2.docx, etc.).

Optional Flags

FlagDescription
-h, --helpShow help message and exit
--versionShow go-template-docx version and exit
--verboseEnable verbose error messages
-i <image.jpg|png>Load an image from disk (can be used multiple times, filenames must be unique)

Examples

Basic usage - Generate a single document:

powershell
./template_docx_windows_amd64.exe template.docx data.json

Generate multiple documents from different JSON files:

powershell
./template_docx_windows_amd64.exe template.docx report1.json report2.json report3.json

Include images in the document:

powershell
./template_docx_windows_amd64.exe template.docx data.json -i logo.png -i photo.jpg

Enable verbose mode for debugging:

powershell
./template_docx_windows_amd64.exe template.docx data.json --verbose

Add to PATH

Adding the executable to your system PATH allows you to run it from any directory without specifying the full path.

Windows

  1. Download the executable and place it in a folder (e.g., C:\Tools\)
  2. Open System Properties:
    • Press Win + R, type sysdm.cpl, and press Enter
    • Or search for "Environment Variables" in the Start menu
  3. Click Environment Variables
  4. Under User variables (or System variables for all users), find and select Path, then click Edit
  5. Click New and add the folder path (e.g., C:\Tools\)
  6. Click OK on all dialogs
  7. Restart any open terminal windows

Alternative using PowerShell (User PATH):

powershell
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
[Environment]::SetEnvironmentVariable("Path", "$userPath;C:\Tools", "User")

macOS

  1. Download the executable and place it in a folder (e.g., ~/bin/)

  2. Make it executable:

    bash
    chmod +x ~/bin/template_docx_darwin_*
  3. Add to PATH by editing your shell configuration file:

    For zsh (default on macOS):

    bash
    echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc

    For bash:

    bash
    echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile

Linux

  1. Download the executable and place it in a folder (e.g., ~/bin/ or /usr/local/bin/)

  2. Make it executable:

    bash
    chmod +x ~/bin/template_docx_linux_amd64
  3. Add to PATH (if using ~/bin/):

    bash
    echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
    source ~/.bashrc

    Or move to a system-wide location:

    bash
    sudo mv template_docx_linux_amd64 /usr/local/bin/template_docx

JSON Data File Format

The JSON file should contain the data to populate your DOCX template. The structure should match the template variables used in your document.

Example data.json:

json
{
  "Title": "Monthly Report",
  "Description": "Sales data for Q4 2024",
  "Author": "John Doe",
  "Items": [
    { "Name": "Product A", "Price": 29.99 },
    { "Name": "Product B", "Price": 49.99 }
  ]
}

This JSON would work with a template containing placeholders like {{.Title}}, {{.Description}}, {{.Author}}, and {{range .Items}}...{{end}}.