docx

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: MIT Imports: 15 Imported by: 0

README

Word(.docx) 文本/图片替换 简单图表生成


import (
	"github.com/mintsec/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)
	
	// Or read from a filesystem object:
	// r, err := docx.ReadDocxFromFS(file string, fs fs.FS)
	
	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", 1)
	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)
	
	r1, _ := docx.ReadDocxFile("./template_1.docx")
	docx3 := r1.Editable()
	// 现已支持png|jpg|svg等格式替换内置png格式
	docx3.ReplaceImage("word/media/gong.png", "./new.png")
	docx3.ReplaceImageByName("攻击趋势","/root/new1.png")
	docx3.ReplaceImageByName("攻击手法分布","/root/new2.svg")
	docx3.WriteToFile("./new_result_3.docx")
	
	r2, _ := docx.ReadDocxFile("./template_2.docx")
	docx4 := r2.Editable()
	
	table := chart.Table{
		Col: []chart.Col{
			{
				Name: "北京",
				Row: []chart.Row{
					{Name: "2022-06-02", Value: 10},
					{Name: "2022-06-03", Value: 12},
					{Name: "2022-06-04", Value: 16},
				},
			},
			{
				Name: "上海",
				Row: []chart.Row{
					{Name: "2022-06-02", Value: 14},
					{Name: "2022-06-03", Value: 9},
					{Name: "2022-06-04", Value: 10},
				},
			},
		},
	}
	_ := docx.RenderChart("图表名称", table)
	r2.Close()
    
	r.Close()
	r1.Close()
}

Documentation

Index

Constants

View Source
const NEWLINE = "<w:br/>"
View Source
const TAB = "</w:t><w:tab/><w:t>"

To get Word to recognize a tab character, we have to first close off the previous text element. This means if there are multiple consecutive tabs, there are empty <w:t></w:t> in between but it still seems to work correctly in the output document, certainly better than other combinations I tried.

Variables

This section is empty.

Functions

This section is empty.

Types

type Docx

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

func (*Docx) GetContent

func (d *Docx) GetContent() string

func (*Docx) RenderChart added in v1.1.2

func (d *Docx) RenderChart(chartName string, table chart.Table) 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 (*Docx) ReplaceImage

func (d *Docx) ReplaceImage(oldImage string, newImage string) (err error)

func (*Docx) ReplaceImageByName

func (d *Docx) ReplaceImageByName(oldImgName, newImgPath 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) 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 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 ReadDocxFromFS

func ReadDocxFromFS(file string, fs fs.FS) (*ReplaceDocx, error)

ReadDocxFromFS opens a docx file from the file system

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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