Skip to content

pduveau/godocx

 
 

Repository files navigation

Godocx

Go CI GitHub go.mod Go version Go Reference Go Report Card License: MIT

Godocx logo

Godocx is a library written in pure Go providing a set of functions that allow you to write to and read from Docx file.

This library needs Go version 1.18 or later. The usage documentation for the godocx can be accessed via the Godocx Documentation Page.

In depth, go docs can be seen using go's built-in documentation tool, or online at go.dev. Please refer the subpackage docx for the list of functions that can be used.

🆕 What's New in This Fork

This fork includes several improvements over the original library:

  • 📑 Table of Contents (TOC): Professional TOC generation with automatic heading detection, dotted leaders, and realistic page numbers
  • ✨ Improved Header/Footer API: Headers and footers now follow the library's fluent API pattern - no manual saving required!
  • 🚀 Better Developer Experience: Consistent with the rest of the library's design philosophy
  • 🔧 Enhanced Examples: Updated examples showing the improved API

Header/Footer Usage Example

package main

import (
    "log"
    "github.com/mrlijnden/godocx"
    "github.com/mrlijnden/godocx/wml/stypes"
)

func main() {
    // Create a new document
    doc, err := godocx.NewDocument()
    if err != nil {
        log.Fatal(err)
    }

    // Add some content
    doc.AddParagraph("This is the main document content.")

    // Create headers and footers with the improved API
    header := doc.AddHeader(stypes.HdrFtrDefault)
    header.AddParagraph("This is a header - no manual save needed!")

    footer := doc.AddFooter(stypes.HdrFtrDefault)
    footer.AddParagraph("This is a footer - no manual save needed!")

    // Everything is automatically saved when you call SaveTo()
    doc.SaveTo("document.docx")
}

Table of Contents Usage Example

package main

import (
    "log"
    "github.com/mrlijnden/godocx"
)

func main() {
    // Create a new document
    doc, err := godocx.NewDocument()
    if err != nil {
        log.Fatal(err)
    }

    // Add content with headings
    doc.AddHeading("Introduction", 1)
    doc.AddParagraph("This is the introduction...")
    
    doc.AddHeading("Getting Started", 1)
    doc.AddParagraph("To get started...")
    
    doc.AddHeading("Installation", 2)
    doc.AddParagraph("Install the library...")

    // Add TOC with fluent API configuration
    toc := doc.AddTableOfContents().
        SetTitle("Table of Contents").
        SetMaxLevel(3).
        SetMinLevel(1).
        SetIncludePageNumbers(true).
        SetIndentation(20)

    // Save the document
    doc.SaveTo("document_with_toc.docx")
}

Usage

Here's a simple example of how you can use Godocx to create and modify DOCX documents:

Installation

Use the godocx in your project

go get github.com/mrlijnden/godocx

Examples

Explore additional examples and use cases over at GitHub repository dedicated to showcasing the capabilities of Golang Docx: https://github.com/gomutex/godocx-examples

Basic examples are included in this repository:

// More examples in separate repository
// https://github.com/gomutex/godocx-examples

package main

import (
	"log"

	"github.com/mrlijnden/godocx"
)

func main() {
		// Open an existing DOCX document
	// document, err := godocx.OpenDocument("./testdata/test.docx")

	// Create New Document
	document, err := godocx.NewDocument()
	if err != nil {
		log.Fatal(err)
	}

	document.AddHeading("Document Title", 0)

	// Add a new paragraph to the document
	p := document.AddParagraph("A plain paragraph having some ")
	p.AddText("bold").Bold(true)
	p.AddText(" and some ")
	p.AddText("italic.").Italic(true)

	document.AddHeading("Heading, level 1", 1)
	document.AddParagraph("Intense quote").Style("Intense Quote")
	document.AddParagraph("first item in unordered list").Style("List Bullet")
	document.AddParagraph("first item in ordered list").Style("List Number")

	records := []struct{ Qty, ID, Desc string }{{"5", "A001", "Laptop"}, {"10", "B202", "Smartphone"}, {"2", "E505", "Smartwatch"}}

	table := document.AddTable()
	table.Style("LightList-Accent4")
	hdrRow := table.AddRow()
	hdrRow.AddCell().AddParagraph("Qty")
	hdrRow.AddCell().AddParagraph("ID")
	hdrRow.AddCell().AddParagraph("Description")

	for _, record := range records {
		row := table.AddRow()
		row.AddCell().AddParagraph(record.Qty)
		row.AddCell().AddParagraph(record.ID)
		row.AddCell().AddParagraph(record.Desc)
	}

	// Save the modified document to a new file
	err = document.SaveTo("demo.docx")
	if err != nil {
		log.Fatal(err)
	}
}

Demo Output

This is screenshot of demo document generated from the godocx library.

Screenshot of the demo output

Feature addition request

If you need a feature that's missing in godocx, feel free to raise an issue describing what you want to achieve, along with a sample DOCX. While I can't promise immediate implementation, I'll review your request and work on it if it's valid.

Inspiration

The Godocx library is inspired from the python-docx

Original Repository

This is a fork of the original gomutex/godocx repository with additional improvements.

Licenses

The Godocx library is licensed under the MIT License.

About

Go library for reading and writing Microsoft Docx

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 100.0%