docx

package module
v0.0.0-...-ce3adc0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2019 License: MIT Imports: 17 Imported by: 0

README

Simple Google Go (golang) library for replace text in microsoft word (.docx) file

The following constitutes the bare minimum required to replace text in DOCX document.


import (
	"github.com/nguyenthenguyen/docx"
)

func main() {
	// Read from docx file
	r, err := docx.ReadDocxFile("./template.docx")
	// Or read from memory
	// r, err := docx.ReadDocxFromMemory(data io.ReaderAt, size int64)
	if err != nil {
		panic(err)
	}
	docx1 := r.Editable()
	// Replace like https://golang.org/pkg/strings/#Replace
	docx1.Replace("old_1_1", "new_1_1", -1)
	docx1.Replace("old_1_2", "new_1_2", -1)
	docx1.ReplaceLink("http://example.com/", "https://github.com/nguyenthenguyen/docx")
	docx1.ReplaceHeader("out with the old", "in with the new")
	docx1.ReplaceFooter("Change This Footer", "new footer")
	docx1.WriteToFile("./new_result_1.docx")

	docx2 := r.Editable()
	docx2.Replace("old_2_1", "new_2_1", -1)
	docx2.Replace("old_2_2", "new_2_2", -1)
	docx2.WriteToFile("./new_result_2.docx")

	// Or write to ioWriter
	// docx2.Write(ioWriter io.Writer)

	r.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHyperlinkNode

func NewHyperlinkNode(text, rID string) (*xmlquery.Node, error)

NewHyperlinkNode todo

func NewHyperlinkRelNode

func NewHyperlinkRelNode(target, rID string) (*xmlquery.Node, error)

NewHyperlinkRelNode todo

Types

type Docx

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

Docx Main docx data structure

func (*Docx) Replace

func (d *Docx) Replace(oldString string, newString string, num int) (err error)

Replace encodes and replaces a string

func (*Docx) ReplaceFooter

func (d *Docx) ReplaceFooter(oldString string, newString string) (err error)

ReplaceFooter replaces in Footer

func (*Docx) ReplaceHeader

func (d *Docx) ReplaceHeader(oldString string, newString string) (err error)

ReplaceHeader replaces in Header

func (d *Docx) ReplaceLink(oldString string, newString string, num int) (err error)

ReplaceLink replaces a link

func (*Docx) ReplaceRaw

func (d *Docx) ReplaceRaw(oldString string, newString string, num int)

ReplaceRaw replaces a raw string

func (*Docx) Write

func (d *Docx) Write(ioWriter io.Writer) (err error)

func (*Docx) WriteToFile

func (d *Docx) WriteToFile(path string) (err error)

WriteToFile writes the docx to a file given the path

type ReplaceDocx

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

ReplaceDocx main data structure

func ReadDocx

func ReadDocx(reader ZipData) (*ReplaceDocx, error)

ReadDocx from zip

func ReadDocxFile

func ReadDocxFile(path string) (*ReplaceDocx, error)

ReadDocxFile reads the docx from a path

func ReadDocxFromMemory

func ReadDocxFromMemory(data io.ReaderAt, size int64) (*ReplaceDocx, error)

ReadDocxFromMemory reads the docx directly from memory

func (d *ReplaceDocx) AddHyperlinkForLink(link string) (*xmlquery.Node, error)

AddHyperlinkForLink adds a new relationship for the link and returns the it

func (*ReplaceDocx) AddInternetLinkStyleIfMissing

func (d *ReplaceDocx) AddInternetLinkStyleIfMissing() error

AddInternetLinkStyleIfMissing looks into the `word/styles.xml` file for InternetLink style, and creates the entry if it does not exist

func (*ReplaceDocx) Close

func (r *ReplaceDocx) Close() error

Close closes the zip reader instance of RelaceDocx

func (*ReplaceDocx) Editable

func (r *ReplaceDocx) Editable() *Docx

Editable Editable data structure

func (d *ReplaceDocx) GetHyperlinkForLink(link string) (*xmlquery.Node, error)

GetHyperlinkForLink looks in document.rel.xml for a relationship that represents an Hyperlink to the given link. If a matching relationship is found it is returned, otherwise is returned the nil value

func (d *ReplaceDocx) GetOrAddHyperlinkForLink(link string) (*xmlquery.Node, error)

GetOrAddHyperlinkForLink looks for an existing relationship for the provided link and if the research is unsuccessfull, it creates and returns a new relationship describing the provided link

func (*ReplaceDocx) GetRelationships

func (d *ReplaceDocx) GetRelationships() (*xmlquery.Node, error)

GetRelationships returns the relationships of the docx file

func (d *ReplaceDocx) ReplaceTextWithHyperlink(lookFor, link string) error

ReplaceTextWithHyperlink todo

func (*ReplaceDocx) SubstituteRunWithHyperlinkWrtTarget

func (d *ReplaceDocx) SubstituteRunWithHyperlinkWrtTarget(chosenOne *xmlquery.Node, target, link string)

SubstituteRunWithHyperlinkWrtTarget todo

type ZipData

type ZipData interface {
	// contains filtered or unexported methods
}

ZipData Contains functions to work with data from a zip file

type ZipFile

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

ZipFile Type for zip files read from disk

type ZipInMemory

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

ZipInMemory Type for in memory zip files

Directories

Path Synopsis
assets
cmd

Jump to

Keyboard shortcuts

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