docx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 14 Imported by: 0

README

A simple go (golang) Microsoft Word (. Docx) tool library

一个简单的golang Word操作库

替换文本

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

func main() {
	//载入word
	doc, rc := docx.LoadInit("./document_test.docx")
	//批量文本替换
	var data = make(map[string]string)
	data["search"] = "批量替换"
	data["search1"] = "批量替换"
	doc.SetValue(data)
	//单独文本替换
	doc.SetValue("search2", "单独替换")
    //保存文件
	doc.SaveToFile("./new_result_2.docx")
	//关闭资源
	rc.Close()
}

复制行

​ CloneRow(mark,num)

编号 姓名
${id} ${name}
import (
	"github.com/wyatsahar/docx"
)

func main() {
    //载入word
	doc, rc := docx.LoadInit("./document_test.docx")
	//复制行
	doc.CloneRow("id", 3)
	//替换复制行后的标签
	var data1 = make(map[string]string)
	data1["id#0"] = "1"
	data1["name#0"] = "张三"
	data1["id#1"] = "2"
	data1["name#1"] = "李四"
	data1["id#2"] = "3"
	data1["name#2"] = "王五"
	doc.SetValue(data1)
    //保存文件
	doc.SaveToFile("./new_result_2.docx")
	//关闭资源
	rc.Close()
}
编号 姓名
1 张三
2 李四
3 王五

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteToString

func ByteToString(b []byte) string

ByteToString 字节转字符串

func LoadInit added in v1.2.0

func LoadInit(path string) (*Docx, *ZipBuffer)

LoadInit 初始化Docx

func StringBuilder

func StringBuilder(s ...string) string

StringBuilder 字符串拼接

Types

type Docx

type Docx struct {
	ZipBuffer        *ZipBuffer
	MainPart         string
	MainPartName     string
	SettingsPart     string
	SettingsPartName string
	ContentTypes     string
	ContentTypesName string
	Headers          map[int]string
	Footers          map[int]string
	Relations        map[string]string
}

Docx 文档

func (*Docx) CloneRow

func (d *Docx) CloneRow(mark string, n int)

CloneRow 复制行 (标记 行数)

func (*Docx) SaveToFile

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

SaveToFile 保存文件

func (*Docx) SetValue

func (d *Docx) SetValue(s ...interface{}) error

SetValue 替换文本

(d *Docx) SetValue( map[search]replace )
(d *Docx) SetValue( search string, replace string)

type ZipBuffer

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

ZipBuffer zip buffer

func (*ZipBuffer) Close

func (b *ZipBuffer) Close() error

Close 关闭

type ZipData

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

ZipData Contains functions to work with data from a zip file

Jump to

Keyboard shortcuts

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