How to use golang for .docx template (table content)

huangapple go评论86阅读模式
英文:

How to use golang for .docx template (table content)

问题

如何使用golang处理.docx模板(表格内容):

像这样(客户数量是动态的):
How to use golang for .docx template (table content)

英文:

How to use golang for .docx template (table content):

Like that (the number of customers is dynamic)
How to use golang for .docx template (table content)

答案1

得分: 4

你可以使用这个简单的包:"github.com/lukasjarosch/go-docx"。该包可以帮助你通过替换{variables}中的文本内容来填充docx文件模板。

示例用法:
How to use golang for .docx template (table content)

填充模板的代码如下:

package main

import (
	"fmt"

	docx "github.com/lukasjarosch/go-docx"
)

func main() {
	replaceMap := docx.PlaceholderMap{
		"_contract_name_": "Home rental",
		"_name_":          "John Doe",
		"_summary_":       "Terms and conditions",
		"_date_":          "13-04-2022",
		"_condition_1_":   "apartment should always be cleaned",
		"_condition_2_":   "term 2 ...",
		"_condition_4_":   "term 4 ...",
		"_condition_3_":   "term 3 ...",
		"_condition_5_":   "term 5 ...",
	}

	for i := 1; i <= 5; i++ {
		replaceMap[fmt.Sprintf("_accept_%d", i)] = "✔️"
		replaceMap[fmt.Sprintf("_reject_%d", i)] = ""
	}

	// 读取和解析模板docx文件
	doc, err := docx.Open("template.docx")
	if err != nil {
		panic(err)
	}

	// 使用replaceMap中的值替换键
	err = doc.ReplaceAll(replaceMap)
	if err != nil {
		panic(err)
	}

	// 写入一个新文件
	err = doc.WriteToFile("replaced.docx")
	if err != nil {
		panic(err)
	}
}

结果文件:
How to use golang for .docx template (table content)

附注:该包不提供插入图片的功能。如果你想要插入图片,可以使用这个商业包:"github.com/unidoc/unioffice/document"

英文:

You can use this simple package: &quot;github.com/lukasjarosch/go-docx&quot;. This package helps you fill docx file templates by replacing {variables} with given text context.

Sample usage:
How to use golang for .docx template (table content)

Code to fill the template:

package main

import (
	&quot;fmt&quot;

	docx &quot;github.com/lukasjarosch/go-docx&quot;
)

func main() {
	replaceMap := docx.PlaceholderMap{
		&quot;_contract_name_&quot;: &quot;Home rental&quot;,
		&quot;_name_&quot;:          &quot;John Doe&quot;,
		&quot;_summary_&quot;:       &quot;Terms and conditions&quot;,
		&quot;_date_&quot;:          &quot;13-04-2022&quot;,
		&quot;_condition_1_&quot;:   &quot;apartment should always be cleaned&quot;,
		&quot;_condition_2_&quot;:   &quot;term 2 ...&quot;,
		&quot;_condition_4_&quot;:   &quot;term 4 ...&quot;,
		&quot;_condition_3_&quot;:   &quot;term 3 ...&quot;,
		&quot;_condition_5_&quot;:   &quot;term 5 ...&quot;,
	}

	for i := 1; i &lt;= 5; i++ {
		replaceMap[fmt.Sprintf(&quot;_accept_%d&quot;, i)] = &quot;✔️&quot;
		replaceMap[fmt.Sprintf(&quot;_reject_%d&quot;, i)] = &quot;&quot;
	}

	// read and parse the template docx
	doc, err := docx.Open(&quot;template.docx&quot;)
	if err != nil {
		panic(err)
	}

	// replace the keys with values from replaceMap
	err = doc.ReplaceAll(replaceMap)
	if err != nil {
		panic(err)
	}

	// write out a new file
	err = doc.WriteToFile(&quot;replaced.docx&quot;)
	if err != nil {
		panic(err)
	}
}

Result file:
How to use golang for .docx template (table content)

P.S: this package does not provide functionality to insert images. If you want to insert images you can use this commercial package: &quot;github.com/unidoc/unioffice/document&quot;

huangapple
  • 本文由 发表于 2022年4月13日 19:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71857097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定