godocx

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 5 Imported by: 3

README

Godocx

Go CI GitHub go.mod Go version Go Reference Go Report Card

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.

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/gomutex/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

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

package main

import (
	"log"

	"github.com/gomutex/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

Inspiration

The Godocx library is inspired from the python-docx

Licenses

The Godocx library is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDocument

func NewDocument() (*docx.RootDoc, error)

NewDocument creates a new document from the default template.

func OpenDocument

func OpenDocument(fileName string) (*docx.RootDoc, error)

OpenDocument opens a document from the given file name.

Types

This section is empty.

Directories

Path Synopsis
common
constants
Package constants provides constant values related to OpenXML namespaces, relationships, and other attributes.
Package constants provides constant values related to OpenXML namespaces, relationships, and other attributes.
dml
Package dml provides functionality for working with DrawingML (Drawing Markup Language) which is part of the Office Open XML (OOXML) standard.
Package dml provides functionality for working with DrawingML (Drawing Markup Language) which is part of the Office Open XML (OOXML) standard.
dmlct
Package dmlct provides complex types related to DrawingML (Drawing Markup Language), which is part of the Office Open XML (OOXML) standard.
Package dmlct provides complex types related to DrawingML (Drawing Markup Language), which is part of the Office Open XML (OOXML) standard.
dmlpic
These elements encompass the definition of pictures within the DrawingML framework.
These elements encompass the definition of pictures within the DrawingML framework.
dmlst
Package dml provides simple types used in DrawingML (Drawing Markup Language), part of the Office Open XML (OOXML) standard for representing graphical elements in documents.
Package dml provides simple types used in DrawingML (Drawing Markup Language), part of the Office Open XML (OOXML) standard for representing graphical elements in documents.
Package docx provides a comprehensive set of functions and structures for manipulating DOCX documents.
Package docx provides a comprehensive set of functions and structures for manipulating DOCX documents.
Package opc provides functionality related to Open Packaging Conventions (OPC).
Package opc provides functionality related to Open Packaging Conventions (OPC).
wml
ctypes
Package ctypes provides complex types used in various components of Office Open XML (OOXML) WordprocessingML (WML) documents.
Package ctypes provides complex types used in various components of Office Open XML (OOXML) WordprocessingML (WML) documents.
stypes
Package stypes provides simple types used in Office Open XML (OOXML) WordprocessingML (WML) documents.
Package stypes provides simple types used in Office Open XML (OOXML) WordprocessingML (WML) documents.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL