Godocx

repository·main·Indexed 42 days ago

https://github.com/gomutex/godocx

A pure Go library for reading from and writing to DOCX files, inspired by python-docx. It provides a high-level API for document manipulation, including functions like NewDocument(), OpenDocument(), AddHeading(), AddParagraph(), and AddTable(). Requires Go version 1.18 or later.

Tokens
1.1K
Snippets
4
Records
4
Agent score
5%

What's inside Godocx

  1. Create and modify DOCX documents with Godocx

    main

    Godocx provides a pure Go API to create new documents or open existing ones. You can add headings, paragraphs with text styling (bold, italic), lists, and tables.

    Key operations include:

    • NewDocument(): Creates a new document instance.
    • OpenDocument(path): Opens an existing DOCX file.
    • AddHeading(text, level): Adds a heading with a specific level.
    • AddParagraph(text): Adds a paragraph. You can chain methods like .Bold(true), .Italic(true), or .Style(name) to the returned paragraph object.
    • AddTable(): Adds a table. You can use .Style(name) and .AddRow() to build the table structure.
    • SaveTo(path): Saves the document to the specified file path.
    package main
    
    import (
    	"log"
    
    	"github.com/gomutex/godocx"
    )
    
    func main() {
    	// Create New Document
    	document, err := godocx.NewDocument()
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	document.AddHeading("Document Title", 0)
    
    	// Add a new paragraph with styling
    	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")
    
    	// Create a table
    	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)
    	}
    }
  2. Create a new DOCX document with NewDocument

    main

    Use NewDocument() to initialize a new *docx.RootDoc instance based on the library's built-in default template. This is the preferred way to start a fresh document from scratch.

    doc, err := godocx.NewDocument()
    if err != nil {
    	// handle error
    }
    // Use doc to manipulate the document
  3. Open an existing DOCX document with OpenDocument

    main

    Use OpenDocument(fileName string) to load an existing DOCX file from the local filesystem into a *docx.RootDoc. The function cleans the provided file path before reading. It returns an error if the file cannot be read or if the content is not a valid DOCX package.

    doc, err := godocx.OpenDocument("path/to/your/document.docx")
    if err != nil {
    	// handle error
    }
    // Use doc to manipulate the document