docx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 13 Imported by: 0

README

Simple Google Go (Golang) library for replacing 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

View Source
const Open = 4
View Source
const Self = 5

Variables

View Source
var (
	Black   = "000000"
	White   = "FFFFFF"
	Red     = "FF0000"
	Lime    = "00FF00"
	Blue    = "0000FF"
	Yellow  = "FFFF00"
	Cyan    = "00FFFF"
	Magenta = "FF00FF"
	Silver  = "C0C0C0"
	Gray    = "808080"
	Maroon  = "800000"
	Olive   = "808000"
	Green   = "008000"
	Purple  = "800080"
	Teal    = "008080"
	Navy    = "000080"
)

Functions

func AtomicWPTokensToString

func AtomicWPTokensToString(token WPTokens) string

func ExtractWPToArrayTextString

func ExtractWPToArrayTextString(wp WP) ([]string, error)

func GetTextFromXML

func GetTextFromXML(src string) (string, error)

func RebuildBlocks

func RebuildBlocks(pattern string, source []string) ([]string, int, error)

RebuildBlocks (pattern string, source []string) (expectedArray []string, blockIDWithTag int, err)

func Screening

func Screening(s string) string

Types

type Block

type Block struct {
	Head   Font
	Body   string
	Footer string
}

type Document

type Document struct {
	WP     []WP
	SectPr SectPr
}

func (*Document) AddNewBlock

func (d *Document) AddNewBlock(s string)

func (*Document) AppendWPBlockInToEnd

func (d *Document) AppendWPBlockInToEnd(block WP)

func (*Document) BodyToString

func (d *Document) BodyToString() string

func (*Document) CreateMarkedStringList

func (d *Document) CreateMarkedStringList(mp MarkerParams, letter ...string) []WP

func (*Document) EditBlockByID

func (d *Document) EditBlockByID(id int)

func (*Document) GetBlockByID

func (d *Document) GetBlockByID(id int) WP

func (*Document) GetBlockIDByTag

func (d *Document) GetBlockIDByTag(tag string) (int, error)

func (*Document) GetCopyBlockByTag

func (d *Document) GetCopyBlockByTag(pattern string) (WP, error)

func (*Document) InsertBlockAfterBlockByID added in v1.1.0

func (d *Document) InsertBlockAfterBlockByID(i int, wp WP) error

func (*Document) RemoveBlockByID added in v1.1.0

func (d *Document) RemoveBlockByID(i int) error

func (*Document) ReplaceTextByTag

func (d *Document) ReplaceTextByTag(pattern string, text string) error

type Docx

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

func (*Docx) AddBlockAfterElement

func (d *Docx) AddBlockAfterElement(elemNum int, element string, body []string) []string

func (*Docx) AddBlockAtTheBeginning

func (d *Docx) AddBlockAtTheBeginning(element string, body []string) []string

func (*Docx) AddBlockAtTheEnd

func (d *Docx) AddBlockAtTheEnd(element string, body []string) []string

func (*Docx) AddBlockBeforeElement

func (d *Docx) AddBlockBeforeElement(elemNum int, element string, body []string) []string

func (*Docx) BlockToString

func (d *Docx) BlockToString(block Block) string
func (d *Docx) BlockToString(block Block) string {
	return fmt.Sprintf("%s%s%s", block.Head, block.Body, block.Footer)
}

func (*Docx) BodyGlue

func (d *Docx) BodyGlue(body []string) string

func (*Docx) BodyParse

func (d *Docx) BodyParse(body string) []string

func (*Docx) Compile

func (d *Docx) Compile(path string, doc Document) error

func (*Docx) CreateNewBlock

func (d *Docx) CreateNewBlock(simpleword string) string

<w:b/>

<w:bCs/>
<w:i/>
<w:iCs/>
<w:color w:val="F10D0C"/>
<w:sz w:val="36"/>
<w:szCs w:val="36"/>
<w:u w:val="single"/>

func (*Docx) CreateStructedBlock

func (d *Docx) CreateStructedBlock(simpleword string, arr ...Font) Block

func (*Docx) EditStructedBlockParams

func (d *Docx) EditStructedBlockParams(block Block) Block

func (*Docx) EditTextInStringBlock

func (d *Docx) EditTextInStringBlock(block string, newText string) string

func (*Docx) GetContent

func (d *Docx) GetContent() string

func (*Docx) GetFirstElementContain

func (d *Docx) GetFirstElementContain(word string, body []string) (int, error)

func (*Docx) GetTextInBlock

func (d *Docx) GetTextInBlock(s string) string

func (*Docx) GlueNodes

func (d *Docx) GlueNodes(header, body, footer string)

func (*Docx) ParseBlockToStruct

func (d *Docx) ParseBlockToStruct(s string) Block

func (*Docx) ParseNode

func (d *Docx) ParseNode() (string, string, string)

func (*Docx) Parser

func (d *Docx) Parser() (Document, error)

func (*Docx) Replace

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

func (*Docx) ReplaceBlockToBlocks

func (d *Docx) ReplaceBlockToBlocks(bodypart []string, id int, blocks []Block) []string

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) ReplaceTextInBlock

func (d *Docx) ReplaceTextInBlock(old, new string, body []string) []string

func (*Docx) ReplaceWithTag

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

func (*Docx) SetContent

func (d *Docx) SetContent(content string)

func (*Docx) Write

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

func (*Docx) WriteToFile

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

type Font

type Font struct {
	FontSize int
	FontName string
	Bold     bool
	Italic   bool
	Strike   bool
	Color    string
	Another  string
}

type MarkerParams

type MarkerParams struct {
}

type RPr

type RPr struct {
	Tag  string
	Body 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 SectPr

type SectPr struct {
	Tag  string
	Body string
}

type WP

type WP struct {
	Tag  string
	Body []WPTokens
}

func BuildArrayTextStringToWP

func BuildArrayTextStringToWP(wp WP, bodyStrings []string) (WP, error)

type WPTokens

type WPTokens struct {
	Tag    string
	Body   string
	Attr   string
	Status int
}

type WR

type WR struct {
	Tag  string
	Body string
}

type WpPr

type WpPr struct {
	Tag  string
	Body string
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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