docx

package module
v0.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: GPL-3.0 Imports: 9 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/test/test.go

package main

import (
   "gitee.com/jn-qq/simple-go-docx"
   "path/filepath"
   "runtime"
)

func main() {
   // 创建docx文档对象
   document := docx.NewDocx()

   //添加段落
   p1 := document.AddParagraph()
   //设置段落格式
   p1.IndFirst().Head(1)
   // 添加文本
   r1 := p1.AddRun("测试所有字体格式")
   // 设置文本格式
   r1.Size(10).Color("FF0000").Font("楷体").
      Shade("clear", "auto", "E7E6E6").
      Bold().Italic().Underline("wave").
      Highlight("yellow")

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

   // 简单连写添加
   document.AddParagraph().Head(2).XSpce(2).AddRun("段落2文本").Color("FF0000").Font("楷体")

   // 自定义样式
   // 声明样式对象
   cs := document.NewCustomStyle("自定义段落样式", "paragraph")
   // 设置具体样式
   cs.CreatePPR().XSpce(2).IndFirst()
   cs.CreateRPR().SetFont("楷体", "楷体", "楷体", "楷体", "eastAsia").SetColor(255, 0, 0).SetSize(10)
   // 添加段落指定段落样式 Head 中的参数要-1
   document.AddParagraph().Head(cs.Id - 1).AddRun("自定义段落样式")

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

   _, 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

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

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

AddParagraph 添加段落

func (*Docx) NewCustomStyle

func (d *Docx) NewCustomStyle(styleName string, styleType string) *styles.CustomStyle

NewCustomStyle 自定义样式

styleName 样式名称
styleType 样式类型,可选:character|paragraph|tab|...

func (*Docx) Save

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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