替换.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?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?