docx

package module
v0.0.0-...-7e82436 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 12 Imported by: 0

README

Simple golang library to replace text in WordprocessingML Office Open XML Format(.docx) files

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


import (
	"github.com/datastyx/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()
}

This fork's main goal is to add two functionalities to nguyenthenguyen/docx library :

  1. adds functionality to remove WordprocessingML content based on an XPath referencing the nodes to be removed, and;
// parse OPC
rodoc, err := docx.ReadDocxFile("./template.docx")
if err != nil {
	panic(err)
}
docx := rodoc.Editable()
err = docx.RemoveElementsFromMainDocument(someXPath)
if err != nil {
	panic(err)
}
  1. Remove CustomXML Parts based on a path referencing it, the paths root shall be the root of the OPC container e.g. 'customXml/item1.xml'.
// parse OPC
rodoc, err := docx.ReadDocxFile("./template.docx")
if err != nil {
	panic(err)
}
docx := rodoc.Editable()
err = docx.RemoveCustomXML("customXml/item1.xml")
if err != nil {
	panic(err)
}

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 (*Docx) GetCustomXML

func (d *Docx) GetCustomXML() map[string]string

GetCustomXML returns a map with key values being a path to a customXML file and the map value the respective content as a string. Result is nil if no customXML parts were retrieved

func (*Docx) GetCustomXMLByRootNs

func (d *Docx) GetCustomXMLByRootNs(rootNS string) (customXmlmap map[string]string, err error)

GetCustomXMLByRootNs returns a map with key values being a path to a customXML Part file in the OPC and the map value the respective content as a string. The map will only contain customXML Parts which XML root namespace matches the given parameter one. Result is nil if no customXML parts were retrieved

func (*Docx) RemoveCustomXML

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

RemoveCustomXML tries to remove a customXml Part from the OPC container. The Part to be removed is designated by the path to 'itemX.xml' file under the 'customXml' directory in the OPC. e.g. to remove 'item1.xml' the given path should be 'customXml/item1.xml'. If the file is refered to from the document.xml the removal is canceled and the method returns an error. This method also removes the following references : the related 'customXml/itemPropsX.xml' file, the related 'customXml/_rels/itemX.xml.rels' and the relationship reference in the 'word/_rels/document.xml.rels' path argument shall be given as an absolute path where the root is that of the OPC. When an error is returned customXml Part is not removed. If the reason is that no customXml part was found for the given path then a 'docx.NoCustomXmlPartError' is returned. If the reason is that the designated customXml Part has existing relationships then a 'docx.ExistingRelationshipsError' is returned

func (*Docx) RemoveElementsFromMainDocument

func (d *Docx) RemoveElementsFromMainDocument(elementXpath string) (err error)

removeElementFromMainDocument finds an element defined by the 'elementXpath' ( if no corresponding element Node found returns a 'NoElementsFoundAtXPath' error).

func (*Docx) Replace

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

func (*Docx) ReplaceFooter

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

func (*Docx) ReplaceHeader

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

func (*Docx) ReplaceRaw

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

func (*Docx) Write

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

func (*Docx) WriteToFile

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

type ExistingRelationshipsError

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

ExistingRelationshipsError is returned when trying to remove a customXML Part that still has existing relationships

func (ExistingRelationshipsError) Error

type NoCustomXmlPartError

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

NoCustomXmlPartError is returned when trying to remove a CustomXML Part that can't be found

func (NoCustomXmlPartError) Error

func (e NoCustomXmlPartError) Error() string

type NoElementsFoundAtXPath

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

func (NoElementsFoundAtXPath) Error

func (e NoElementsFoundAtXPath) Error() string

type ReplaceDocx

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

func ReadDocx

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

func ReadDocxFile

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

func ReadDocxFromMemory

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

func (*ReplaceDocx) Close

func (r *ReplaceDocx) Close() error

func (*ReplaceDocx) Editable

func (r *ReplaceDocx) Editable() *Docx

type ZipData

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

Contains functions to work with data from a zip file

type ZipFile

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

Type for zip files read from disk

type ZipInMemory

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

Type for in memory zip files

Jump to

Keyboard shortcuts

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