DEV Community

Misael Monterroca
Misael Monterroca

Posted on

Show & Tell: go-docx v2.0.0 - Create & modify Word documents in Go

Hi Gophers,

After months of work, I'm excited to share go-docx v2.0.0 - a production-ready library for creating and modifying Word documents in Go!

What It Does

Generate professional .docx files programmatically - perfect for reports, invoices, contracts, documentation, or any automated document workflow.

Now with document reading! Open existing .docx files, modify content, and save changes.

Key Features

Content Creation:

  • Paragraphs with full formatting (alignment, spacing, indentation)
  • Text runs (bold, italic, colors, fonts, sizes, highlights)
  • Advanced tables (cell merging, borders, shading, 8 built-in styles)
  • Images (9 formats: PNG, JPEG, GIF, SVG, etc.)
  • 40+ built-in Word styles (Heading1-9, Title, Quote, etc.)

Document Reading (NEW!):

  • Open existing .docx files
  • Read & modify paragraphs, runs, tables
  • Preserve styles and formatting
  • Round-trip: Create -> Save -> Open -> Modify -> Save

Architecture:

  • Domain-driven design
  • Comprehensive error handling
  • Type-safe (no interface{})
  • Thread-safe with RWMutex
  • Zero linter warnings (30+ linters)

Quick Example

package main

import (
    "log"
    docx "github.com/mmonterroca/docxgo"
    "github.com/mmonterroca/docxgo/domain"
)

func main() {
    // Simple API - Direct
    doc := docx.NewDocument()

    para, _ := doc.AddParagraph()
    para.SetStyle(domain.StyleIDHeading1)

    run, _ := para.AddRun()
    run.SetText("Hello, World!")
    run.SetBold(true)
    run.SetColor(domain.Color{R: 0, G: 112, B: 192})

    doc.SaveAs("report.docx")
}
Enter fullscreen mode Exit fullscreen mode

Builder API (Fluent & Chainable)

builder := docx.NewDocumentBuilder(
    docx.WithTitle("My Report"),
    docx.WithAuthor("Jane Doe"),
)

builder.AddParagraph().
    Text("Project Report").
    Bold().
    FontSize(16).
    Color(docx.Blue).
    Alignment(domain.AlignmentCenter).
    End()

builder.AddTable(3, 3).
    HeaderRow(true).
    Style(docx.StyleTableGrid).
    End()

doc, _ := builder.Build()
doc.SaveAs("report.docx")
Enter fullscreen mode Exit fullscreen mode

Read & Modify Documents

// Open existing document
doc, _ := docx.OpenDocument("template.docx")

// Find and replace text
for _, para := range doc.Paragraphs() {
    for _, run := range para.Runs() {
        if run.Text() == "PLACEHOLDER" {
            run.SetText("Updated Value")
            run.SetBold(true)
        }
    }
}

// Add new content
newPara, _ := doc.AddParagraph()
newRun, _ := newPara.AddRun()
newRun.SetText("This was added by code")

doc.SaveAs("modified.docx")
Enter fullscreen mode Exit fullscreen mode

Installation

go get github.com/mmonterroca/docxgo@v2.0.0
Enter fullscreen mode Exit fullscreen mode

Resources

Real-World Use Cases

  • Invoice/billing generation - Automated invoices with tables and company branding
  • Report generation - Weekly/monthly reports with charts and tables
  • Contract automation - Fill templates with client data
  • Technical documentation - Generate specs with code examples and diagrams
  • Academic papers - Automated formatting with citations and references

Technical Details

  • Go 1.23+
  • Full OOXML support (ISO/IEC 29500)
  • Compatible with: Word 2007+, LibreOffice, Google Docs
  • 50.7% test coverage (improvement plan to 95%)
  • 11/11 examples working - All generate valid documents

Breaking Changes from v1.x

Complete API redesign - v2.0.0 is interface-based with explicit error handling. See migration guide for details.

Roadmap

v2.1.0 (Q1 2026):

  • Complete document reading (headers, footers, images)
  • Comments and change tracking

v2.2.0 (Q2 2026):

  • Custom XML parts
  • Advanced shapes
  • Content controls

Would love to hear your feedback, use cases, or feature requests!

Built on top of the original fumiama/go-docx, completely rewritten with modern Go practices.

Top comments (1)

Collapse
 
mmonterroca_8 profile image
Misael Monterroca

Quick comparison with other Go document libraries:

go-docx v2.0.0:

  • Read & modify existing documents
  • 40+ built-in styles
  • Advanced tables (cell merging)
  • Domain-driven design
  • Comprehensive error handling
  • Two APIs (Simple + Builder)

unioffice (unidoc):

  • Commercial support
  • Paid license required
  • More features (PDF, Excel)

gingk/go-docx:

  • Lightweight
  • Limited features
  • Basic tables only

Happy to discuss trade-offs or answer questions!