golang 解析处理word文档扩展包
github.com/unidoc/unioffice
该扩展包对word操作功能比较全,但为商业使用,注册后有100次的试用,具体使用就不详细说明了,具体可以看 https://github.com/unidoc/unioffice-examples
github.com/carmel/gooxml
该扩展包为unidoc/unioffice的免费版,为收费版的1.4.0版本,虽然功能没有收费版多,但已经涵盖了大部分的基本功能
将包导入到自己的项目
go get github.com/carmel/gooxml
基本使用
1. 创建文档并填写内容
doc := document.New()
para := doc.AddParagraph() // 新增段落
run := para.AddRun()
run.AddText("这里是段落文字信息") // 添加文字信息
// 如果想对添加的文字信息进行换行处理,使用'\r'
run.AddText("这里第一行段落文字信息\r这里是第二行段落文字信息")
doc.SaveToFile("simple.docx") // 保存文件路径,此处应为绝对路径
2. 给段落添加各种样式
para.SetStyle("Title")
para.SetStyle("Heading1") // Heading1 Heading2 Heading3
para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) // 段落添加首行缩进
para.Properties().AddSection(wml.ST_SectionMarkNextPage) // 另起一页(用在AddText之后)
3. 给文字添加各种样式
run.Properties().SetBold(true) // 是否加粗
run.Properties().SetFontFamily("Courier") // 字体
run.Properties().SetSize(15) // 字号
run.Properties().SetColor(color.Red) // 文字颜色
run.Properties().SetKerning(5) // 文字字距
run.Properties().SetCharacterSpacing(5) // 字符间距调整
run.Properties().SetHighlight(wml.ST_HighlightColorYellow) // 设置高亮
run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red) // 下划线
4. 设置页眉页脚
hdr := doc.AddHeader()
para := hdr.AddParagraph()
para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
run := para.AddRun()
run.AddTab()
run.AddText("My Document Title")
ftr := doc.AddFooter()
para = ftr.AddParagraph()
para.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)
run = para.AddRun()
run.AddText("Some subtitle goes here")
run.AddTab()
5. 添加图片
// 初始化图片信息
img1, err := common.ImageFromFile("图片绝对路径")
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
img1ref, err := doc.AddImage(img1)
if err != nil {
log.Fatalf("unable to add image to document: %s", err)
}
// 将图片添加到对应的段落
para := doc.AddParagraph()
anchored, err := para.AddRun().AddDrawingAnchored(img1ref)
if err != nil {
log.Fatalf("unable to add anchored image: %s", err)
}
// 设置图片相关样式
anchored.SetName("图片名称")
anchored.SetSize(2*measurement.Inch, 2*measurement.Inch)
anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin)
anchored.SetHAlignment(wml.WdST_AlignHCenter)
anchored.SetYOffset(3 * measurement.Inch)
anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides)
6. word模板替换
该扩展包还支持word模板替换,提前设置好样式模板,然后替换对应的文字
doc, err := document.Open("document_template.docx") // 获取模板文档路径
if err != nil {
log.Fatalf("error opening document: %s", err)
}
// 获取全部的段落信息
paragraphs := []document.Paragraph{}
for _, p := range doc.Paragraphs() {
paragraphs = append(paragraphs, p)
}
for _, sdt := range doc.StructuredDocumentTags() {
for _, p := range sdt.Paragraphs() {
paragraphs = append(paragraphs, p)
}
}
// 循环段落并进行文字替换
for _, p := range paragraphs {
for _, r := range p.Runs() {
switch r.Text() {
case "模板中的文字":
r.ClearContent() // 清除原有的文字信息和换行符
r.AddText("替换的文字")
r.AddBreak()
// 在该段落前添加新段落
para := doc.InsertParagraphBefore(p)
para.AddRun().AddText("Mr.")
// 在该段落后添加新段落
para = doc.InsertParagraphAfter(p)
para.AddRun().AddText("III")
}
}
}
doc.SaveToFile("edit-document.docx") // 生成替换后的新文档
7. 文档水印
该版本暂无添加水印功能,不过可以使用模板替换功能实现带水印的word文档
doc, err := document.Open("document_template.docx") // 该模板是一个已经带水印的模板
// 添加文字内容
para := doc.AddParagraph()
run := para.AddRun()
run.AddText(content)
doc.SaveToFile("edit-document.docx")
// 如果要使用下面的方法添加一个空白页,则空白页不会附带设置好的水印,如需新增新页面,则模板需要设置两个带水印的空白页面
para.Properties().AddSection(wml.ST_SectionMarkNextPage)
分类:
golang
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!