docx

package module
v1.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

README

simple-go-docx

介绍

golang 生成简单docx文档,纯文本样式


包结构
graph LR
    A(simple-go-docx)--package-->B((docx))
    A--dir-->C{{document}}--package-->D((document))
    A--dir-->E{{paragraph}}--package-->F((paragraph))
    A--dir-->G{{run}}--package-->H((run))
    A--dir-->I{{styles}}--package-->J((styles))
    A--dir-->K[(templates)]
安装
go get -u gitee.com/jn-qq/simple-go-docx

使用说明

simple-go-docx/docx_test.go

    // 创建docx文档对象
	document := NewDocx()

	// 修改默认样式
	defaultStyle := document.GetStyle("Normal")
	defaultStyle.TextStyle.SetFont("楷体")

	//添加段落 ①
	p1 := document.AddParagraph()
	//设置段落格式
	p1.Style.IndFirst()
	// 添加文本 ①
	r1 := p1.AddText("测试所有字体格式")
	// 设置文本格式
	r1.Style.SetSize(shared.Pt(10)).SetColor("FF0000").SetFont("楷体").
		SetBold().SetItalic().SetUnderLine("wave").
		HighlightColor(shared.ColorLib.Yellow)

	// 添加文本 ②
	p1.AddText("段落新增文本1")
	p1.AddText("段落新增文本2")

	// 简单连写添加
	p2 := document.AddParagraph()
	p2.Style.SetHead(2).XLineSpace(2)
	p2.AddText("段落2文本").Style.SetColor("FF0000").SetFont("楷体")

	// 自定义样式
	style := styles.NewCustomStyle("自定义样式1", "paragraph")
	// 设置具体样式
	style.ParagraphStyle.IndFirst().XLineSpace(2)
	style.TextStyle.SetFont("楷体").SetSize(shared.Pt(20)).SetColor(shared.ColorLib.Blue)
	// 添加声明样式 获取id
	sid := document.AddCustomStyle(&style)
	// 添加段落指定段落样式
	p3 := document.AddParagraph()
	p3.Style.SetHead(sid)
	p3.AddText("自定义段落样式")

	// 添加字符样式
	//cs := docx.NewCustomStyle("自定义段落样式", "character")
	//添加属性。。。

	// 图片相关操作
	// 上传图片
	if err := document.UploadImages(10, &image.Image{
		Online: "https://tse4-mm.cn.bing.net/th/id/OIP-C.4UlvcR0AB1Oh_iXwP7szowHaGI",
		Name:   "image1",
	}); err != nil {
		panic(err)
	}
	p4 := document.AddParagraph()
	p4.AddDrawing("image1", shared.Cm(5), shared.Cm(5), "")
	p4.AddDrawing("image1", shared.Cm(5), shared.Cm(5), "")

	_, path, _, _ := runtime.Caller(0)
	path, _ = filepath.Split(path)

	err := document.Save(filepath.Join(path, "test.docx"))
	if err != nil {
		panic(err)
	}


参考
  1. go-docx

  2. ooxml

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Docx

type Docx struct {
	Document document.Document // word/document.xml 文档内容
	// contains filtered or unexported fields
}

func NewDocx

func NewDocx() *Docx

NewDocx 生成一个新的空 A4 docx 文件,我们可以对其进行操作和保存

func (*Docx) AddCustomStyle

func (d *Docx) AddCustomStyle(style *styles.CustomStyle) int64

AddCustomStyle 添加自定义样式

styleType 样式类型,可选:character|paragraph|tab|...
Example
// 创建docx文档对象
document := NewDocx()
defer document.Save("test.docx")

// 创建自定义样式对象
style := styles.NewCustomStyle("ct1", "paragraph")

// 设置段落文本格式
style.ParagraphStyle.IndFirst().XLineSpace(2)
style.TextStyle.SetFont("楷体").SetSize(shared.Pt(20)).SetColor(shared.ColorLib.Blue)

// 添加声明样式到文档 获取系统分配id
sid := document.AddCustomStyle(&style)

//添加段落指定段落样式 Head 中的参数要-1
p := document.AddParagraph()
p.Style.SetHead(sid)
p.AddText("自定义段落样式")
Output:

func (*Docx) AddParagraph

func (d *Docx) AddParagraph() *paragraph.Paragraph

AddParagraph 添加段落,可对该段落设置样式

Example
// 创建docx文档对象
document := NewDocx()
defer document.Save("test.docx")

// 声明段落 可添加格式文本...
p1 := document.AddParagraph()
p1.AddText("测试段落。")
Output:

func (*Docx) GetStyle

func (d *Docx) GetStyle(styleName string) *styles.CustomStyle

GetStyle 获取已存在的样式

Example
// 创建docx文档对象
document := NewDocx()
defer document.Save("test.docx")

// 获取默认样式 Normal | heading 1-4 | character
defaultStyle := document.GetStyle("Normal")
defaultStyle.TextStyle.SetFont("楷体")

// 声明段落 可添加格式文本...
p1 := document.AddParagraph()
p1.AddText("修改默认文本字体。")
Output:

func (*Docx) Save

func (d *Docx) Save(savePath string) error

Save 保存docx文档

savePath:文档保存路径,仅支持.docx文档

func (*Docx) UploadImages

func (d *Docx) UploadImages(quality int, imgList ...*image.Image) error

UploadImages 批量上传图片

q: 图片质量 1-100,默认100
Example
// 创建docx文档对象
document := NewDocx()
defer document.Save("test.docx")

// 上传图片,支持本地、网络及数据流,图片质量50
if err := document.UploadImages(
	50,
	&image.Image{
		Online: "https://tse4-mm.cn.bing.net/th/id/OIP-C.4UlvcR0AB1Oh_iXwP7szowHaGI",
		Name:   "image1",
	},
	&image.Image{
		Local: ".../test.png",
		Name:  "image2",
	},
	&image.Image{
		Bytes: []byte{},
		Name:  "image3",
	},
); err != nil {
	panic(err)
}

// 使用 创建段落 -> 映射图片 -> 设置题注。。。

// 图片②③同行居中显示
p1 := document.AddParagraph()
p1.AddDrawing("image2", shared.Cm(5), shared.Cm(5), shared.AlignArguments.Center)
p1.AddDrawing("image3", shared.Cm(5), shared.Cm(5), shared.AlignArguments.Center)

// 题注
t := document.AddParagraph()
t.Style.Align(shared.AlignArguments.Center)
t.AddText("图1 两个同行显示图").Style.SetSize(shared.Wx(10))
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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