替换.docx文件中文本的简单Go库

用于替换 Microsoft Word (.docx) 文件中文本的简单 Google Go (Golang) 库。「Simple Google Go (Golang) library for replacing text in Microsoft Word (.docx) file」

  • 所有者: nguyenthenguyen/docx
  • 平台: Linux,Mac,Windows
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Simple Google Go (Golang) library for replacing text in Microsoft Word (.docx) file

The following constitutes the bare minimum required to replace text in DOCX document.


package main

import (
	"strconv"

	"github.com/nguyenthenguyen/docx"
)

func main() {
	// Read from docx file
	r, err := docx.ReadDocxFile("./TestDocument.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)

	docx3 := r.Editable()
	//Currently only swaps apples for apples i.e. png to png, and not png to jpeg etc.
	docx3.ReplaceImage("word/media/image1.png", "./new.png")

	// replace the last image
	imageIndex := docx3.ImagesLen()
	docx3.ReplaceImage("word/media/image"+strconv.Itoa(imageIndex)+".png", "./new.png")
	docx3.WriteToFile("./new_result_3.docx")

	r.Close()
}

主要指标

概览
名称与所有者nguyenthenguyen/docx
主编程语言Go
编程语言 (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-10-09 19:24:50
推送于2024-07-29 10:30:49
最后一次提交2023-06-21 18:21:18
发布数1
最新版本名称v1.0 (发布于 )
第一版名称v1.0 (发布于 )
用户参与
星数587
关注者数11
派生数140
提交数50
已启用问题?
问题数18
打开的问题数7
拉请求数13
打开的拉请求数6
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?