docx

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2021 License: MIT Imports: 19 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 王五
替换图片
import (
	"github.com/wyatsahar/docx"
)

func main (){
    //载入word
	doc, rc := docx.LoadInit("./document_test.docx")
    
    //增加图片 设置宽高
	img1 := doc.GetArrangeImage("./aaa.png").SetHeight(100).SetWidth(100)
	img2 := doc.GetArrangeImage("./bbb.jpg").SetHeight(150).SetWidth(200)
	//替换图片
	doc.SetImagesValues("img", img1)
	doc.SetImagesValues("img3", img2)
	doc.SetImagesValues("img2", img1)
    //保存文件
	doc.SaveToFile("./new_result_2.docx")
	//关闭资源
	rc.Close()
}

Documentation

Index

Constants

View Source
const (
	GIFTYPE = "image/gif"
	BMPTYPE = "image/x-ms-bmp"
	JPGTYPE = "image/jpeg"
	PNGTYPE = "image/png"
)

GIFTYPE

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
	NewImages        map[string]ImgValue
}

Docx 文档

func (*Docx) CloneRow

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

CloneRow 复制行 (标记 行数)

func (*Docx) GetArrangeImage

func (d *Docx) GetArrangeImage(path string) ImgValue

GetArrangeImage return imgValue

func (*Docx) SaveToFile

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

SaveToFile 保存文件

func (*Docx) SetImagesValues

func (d *Docx) SetImagesValues(search string, img ImgValue)

SetImagesValues 设置图片

图片 header document foot 写的位置都是不一样的
所以只有在设置的时候能查到

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 ImgValue

type ImgValue struct {
	Path, Type string
	Width      int
	Height     int
	Search     string
	Replace    string
	Rid        string
}

ImgValue 结构体

func (ImgValue) SetHeight

func (i ImgValue) SetHeight(height int) ImgValue

SetHeight 设置图片高度度

func (ImgValue) SetWidth

func (i ImgValue) SetWidth(width int) ImgValue

SetWidth 设置图片宽度

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