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
| Flag | Description |
|---|---|
-h, --help | Show help message and exit |
--version | Show go-template-docx version and exit |
--verbose | Enable 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:
./template_docx_windows_amd64.exe template.docx data.jsonGenerate multiple documents from different JSON files:
./template_docx_windows_amd64.exe template.docx report1.json report2.json report3.jsonInclude images in the document:
./template_docx_windows_amd64.exe template.docx data.json -i logo.png -i photo.jpgEnable verbose mode for debugging:
./template_docx_windows_amd64.exe template.docx data.json --verboseAdd to PATH
Adding the executable to your system PATH allows you to run it from any directory without specifying the full path.
Windows
- Download the executable and place it in a folder (e.g.,
C:\Tools\) - Open System Properties:
- Press
Win + R, typesysdm.cpl, and press Enter - Or search for "Environment Variables" in the Start menu
- Press
- Click Environment Variables
- Under User variables (or System variables for all users), find and select
Path, then click Edit - Click New and add the folder path (e.g.,
C:\Tools\) - Click OK on all dialogs
- Restart any open terminal windows
Alternative using PowerShell (User PATH):
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
[Environment]::SetEnvironmentVariable("Path", "$userPath;C:\Tools", "User")macOS
Download the executable and place it in a folder (e.g.,
~/bin/)Make it executable:
bashchmod +x ~/bin/template_docx_darwin_*Add to PATH by editing your shell configuration file:
For zsh (default on macOS):
bashecho 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc source ~/.zshrcFor bash:
bashecho 'export PATH="$HOME/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile
Linux
Download the executable and place it in a folder (e.g.,
~/bin/or/usr/local/bin/)Make it executable:
bashchmod +x ~/bin/template_docx_linux_amd64Add to PATH (if using
~/bin/):bashecho 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc source ~/.bashrcOr move to a system-wide location:
bashsudo 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:
{
"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}}.