Generate and Parse .docx Files via Free Go Library
Open Source Go Word Processing Library to Programmatically Read & Write Microsoft Word. Insert Media Files, Images, Text, Headers/Footers, and Tables to DOCX Files.
What is Go-Docx Library?
In the world of Golang development, handling Microsoft Word documents (.docx) has historically been a challenge. Many existing libraries are either restricted to "write-only" functionality or come with a hefty commercial price tag. Go-Docx (maintained by fumiama) bridges this gap as one of the most functional, open-source libraries designed to both read and write ECMA-376 Office Open XML files. It is useful because it provides a high-level API that abstracts the complex XML structures underlying Word files
What makes Go-Docx stand out is its comprehensive feature set: text formatting (color, size, alignment), picture insertion, table manipulation, shapes, canvases, and groups. Whether you're generating invoices, parsing resumes, or automating report generation, this library handles complex Office Open XML (ECMA-376) structures without the overhead of calling external applications. It is a community-driven fork that has evolved significantly from its predecessors. Unlike commercial alternatives like UniOffice (which requires payment for full features), Go-Docx remains completely free under the AGPL-3.0 license.
Getting Started with Go-Docx
The recommended way Go-Docx into your project is by using GitHub. Please use the following command for a smooth installation.
Install Go-Docx via GitHub
go get -d github.com/fumiama/go-docx@latest Advanced Document Generation via Go
Go-Docx allows you to create complex documents from scratch using a structured approach. It handles the initialization of the document environment, including default themes and styles, ensuring that your generated files are fully compatible with Microsoft Word and other modern processors. Here is a simple example that demonstrates how to generate a Word Docx file inside Go apps.
How to Generate Word Docx Files via Go Library?
package main
import (
"os"
"github.com/fumiama/go-docx"
)
func main() {
// Initialize a new document with a default theme
w := docx.New().WithDefaultTheme()
// Add a paragraph and basic text
para := w.AddParagraph()
para.AddText("Hello, this is a generated document!")
// Save to local file
f, _ := os.Create("simple.docx")
w.WriteTo(f)
f.Close()
}
Comprehensive Document Parsing via Go API
Unlike many competitors, the open source Go-Docx library excels at reading existing files. It can parse a .docx file and iterate through its body items, such as paragraphs and tables. This makes it an ideal choice for data extraction or document auditing tasks where you need to analyze content programmatically. The following example shows how software developers can parse word documents inside their own Go applications.
How to Parse Word Docx Documents via Go Library?
package main
import (
"fmt"
"os"
"github.com/fumiama/go-docx"
)
func main() {
readFile, _ := os.Open("existing.docx")
info, _ := readFile.Stat()
// Parse the file using its reader and size
doc, _ := docx.Parse(readFile, info.Size())
for _, item := range doc.Document.Body.Items {
if p, ok := item.(*docx.Paragraph); ok {
fmt.Println("Paragraph found:", p)
}
}
}
Dynamic Table Construction in Docx Files via Go
Open source Go-Docx library has included support for the creation and manipulation of tables, including nested tables—a feature often missing in simpler libraries. You can define rows, cells, and specific border styles, making it highly effective for generating financial reports, invoices, or data-heavy technical documentation. Here is a very useful code example that shows how to create a table inside Word documents via Go commands.
How to Create a Simple Table inside Word File via Go Library?
func createTable(w *docx.Docx) {
table := w.AddTable(2, 2) // Create a 2x2 table
row := table.Rows[0]
cell := row.Cells[0]
p := cell.AddParagraph()
p.AddText("Cell Content")
// You can also adjust table properties here
}
Rich Text Styling and Formatting Support
Fine-grained control over typography is a core strength of this open source Go library. You can modify text color, font size, alignment, and even add hyperlinks or tab stops via Go-Docx. This allows developers to generate professional, branded documents that match specific design requirements without manual intervention.
How to Apply Text Styling and Formatting to Word Documents via Go Library?
func styleText(w *docx.Docx) {
para := w.AddParagraph()
// Chain styling methods for concise code
para.AddText("Important Header").Size("32").Color("FF0000")
para.AddText("Subtext").Italic().Bold()
// Add a link
para.AddLink("Visit Website", "https://example.com")
}