A Go library for creating, modifying, and rendering DOCX documents. Handles everything from generating new files to filling templates with data, without mutating your document styles.
docforge is under active development. See the changelog for current status.
go get github.com/vukan322/docforgeFor basic placeholder replacement without a full template engine:
doc, err := docforge.Open("template.docx")
if err != nil {
log.Fatal(err)
}
err = doc.Replace(map[string]any{
"Name": "Luka",
"Company": "Kantech",
})
if err != nil {
log.Fatal(err)
}
err = doc.Save("output.docx")
if err != nil {
log.Fatal(err)
}In your Word template use {{.Name}} and {{.Company}} as placeholders.
For range, conditionals, and custom functions:
doc, err := docforge.Open("template.docx")
if err != nil {
log.Fatal(err)
}
doc.AddFunc("formatDate", func(t time.Time) string {
return t.Format("02.01.2006")
})
err = doc.Render(map[string]any{
"Name": "Luka",
"Date": time.Now(),
"Items": []map[string]any{
{"Product": "Valve", "Qty": 3},
{"Product": "Pump", "Qty": 1},
},
}, "output.docx")
if err != nil {
log.Fatal(err)
}Use the docforge struct tag to control placeholder names:
type Invoice struct {
FirstName string `docforge:"first_name"`
Amount float64 `docforge:"amount"`
}{{.first_name}} and {{.amount}} in your template will map to the struct fields.
Without a tag, the field name is used as-is.
| Syntax | Purpose |
|---|---|
{{.FieldName}} |
Simple value replacement |
{{range .Items}} ... {{end}} |
Table row iteration |
{{if .Flag}} ... {{end}} |
Conditional content |
{{.Date | formatDate}} |
Custom function |
This feature is planned for v2. See the changelog for current status. The API below is a preview and not yet implemented.
doc := docforge.New()
doc.AddParagraph("Hello from docforge")
doc.AddTable([][]string{
{"Name", "Quantity"},
{"Valve", "3"},
{"Pump", "1"},
})
err := doc.Save("output.docx")
if err != nil {
log.Fatal(err)
}This project is licensed under the MIT License - see the LICENSE file for details.