docx

package module
v0.0.0-...-8e6e6c1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 8 Imported by: 0

README

docx

Docx documents template processor (not old format doc files)

Uses text/template to do replacements

Example:

package main

import (
	"github.com/msilvoso/docx"
        "log"
)

func main() {
    // Docx with "{{.placeholder1}} {{.placeholder2}}{{.placeholder3}}"
    // becomes "Hello World!"
    replacements := map[string]string {
        "placeholder1": "Hello",
        "placeholder2": "World",
        "placeholder3": "!",
    }   

    // load word document
    d, err := docx.New("wordfile.docx")
    if err != nil {
        log.Fatalln("%s\n", err.Error())
    }
    
    // replace placeholders
    d.Replace(replacements)

    // Save resulting docx to file
    d.SaveDocxToFile("replaced.docx")
 
    d.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Docx

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

func New

func New(path string) (Docx, error)

func (*Docx) Close

func (d *Docx) Close()

func (*Docx) CreateNewDocx

func (d *Docx) CreateNewDocx() error

create the resulting docx and store the byte slice to result

func (*Docx) LoadDocx

func (d *Docx) LoadDocx(path string) error

load the docx file an extract the document.xml file TODO: replace file by io.reader?

func (*Docx) NewDocx

func (d *Docx) NewDocx() []byte

create the resulting docx, store the byte slice to result and return it

func (*Docx) Replace

func (d *Docx) Replace(replacements map[string]string) (err error)

replace placeholders using the text/template package this function does the escaping for you using html/template

func (*Docx) ReplaceCond

func (d *Docx) ReplaceCond(replacements map[string]Replacement) (err error)

ReplaceCond replaces replaces the placeholders like the other two replacement functions but provides the possibility to choose (with the escaped field) whether the string should be escaped

func (*Docx) SaveDocxToFile

func (d *Docx) SaveDocxToFile(path string) error

Save the resulting docx to a file

type Replacement

type Replacement struct {
	ReplacementValue string
	Escaped          bool
}

Jump to

Keyboard shortcuts

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