docx

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 6 Imported by: 0

README

Docx

Docx is a simple package for for filling out documents prepared in Office Open XML format, usually saved as a filename with .docx extension.

It looks for variables in text and replaces them with defined values.

You can check example template document.

Installation

go get -u github.com/elblox/go-docx

Usage

	dict := map[string]string{
		"[variable1]": "Some text",
		"[variable2]": "Some other text",
	}
	output, err := os.Create("output.docx")
	if err != nil {
		return err
	}
	input, err := os.Open("input.docx")
	if err != nil {
		return err
	}
	stat, err := input.Stat()
	if err != nil {
		return err
	}
	// we need file size because of zip.Reader
	_, err = docx.New(input, stat.Size()).
		Replace(dict).
		WriteTo(output)
	if err != nil {
		return err
	}

You can also check docx_test.go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer []xml.Token

Buffer is a slice of XML tokes which are buffered before saving them in a file

func (*Buffer) Clean

func (buffer *Buffer) Clean()

Clean removes tokens from a buffer and keeps capacity untouched

func (*Buffer) Flush

func (buffer *Buffer) Flush(encoder *xml.Encoder) error

Flush saves all tokens to XML file and cleans the buffer

func (*Buffer) Process

func (buffer *Buffer) Process(encoder *xml.Encoder, dict Dict) error

Process converts CharData tokens from a buffer to one string and replaces variables with values from a dictionary

type Dict

type Dict map[string]string

Dict is a dictionary with variables and values to which they should be replaced

type Docx

type Docx struct {
	// contains filtered or unexported fields
}

Docx can manipulate .docx files created by MS Word 2007+

func New

func New(r io.ReaderAt, size int64) *Docx

New creates Docx instance

func (*Docx) Brackets

func (doc *Docx) Brackets(opening, closing rune) *Docx

Brackets allows to configure characters which symbolice start and end of variable

func (*Docx) Replace

func (doc *Docx) Replace(dict map[string]string) *Docx

Replace stores dictionary of words to replace

func (*Docx) WriteTo

func (doc *Docx) WriteTo(w io.Writer) (int64, error)

WriteTo puts ZIP content to given writer (like a file of HTTP response)

Jump to

Keyboard shortcuts

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