2025-02-19 18:30:19 +08:00
|
|
|
package wps
|
|
|
|
|
|
|
|
import (
|
2025-02-26 18:58:43 +08:00
|
|
|
"dt_automate/tool"
|
2025-02-19 18:30:19 +08:00
|
|
|
"log"
|
2025-02-25 18:36:33 +08:00
|
|
|
"strings"
|
2025-02-25 20:31:44 +08:00
|
|
|
"time"
|
2025-02-19 18:30:19 +08:00
|
|
|
|
2025-02-25 18:36:33 +08:00
|
|
|
"github.com/unidoc/unioffice/common"
|
|
|
|
"github.com/unidoc/unioffice/common/license"
|
|
|
|
"github.com/unidoc/unioffice/document"
|
|
|
|
"github.com/unidoc/unioffice/measurement"
|
|
|
|
"github.com/unidoc/unioffice/schema/soo/wml"
|
2025-02-19 18:30:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 设置unidoc key
|
|
|
|
func init() {
|
|
|
|
// Make sure to load your metered License API key prior to using the library.
|
|
|
|
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
|
|
|
|
err := license.SetMeteredKey(`4d568da2fb25ed2477068464ffde96b7e7fa091595e6af3610eb4aee1b539a24`)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2025-02-25 18:36:33 +08:00
|
|
|
|
2025-02-19 18:30:19 +08:00
|
|
|
func Word() {
|
2025-02-25 18:36:33 +08:00
|
|
|
doc, err := document.Open("temp.docx")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("打开文件失败", err)
|
|
|
|
}
|
2025-02-19 18:30:19 +08:00
|
|
|
defer doc.Close()
|
2025-02-25 18:36:33 +08:00
|
|
|
// 填充模板中的变量
|
|
|
|
paragraphs := []document.Paragraph{}
|
|
|
|
for _, p := range doc.Paragraphs() {
|
|
|
|
paragraphs = append(paragraphs, p)
|
|
|
|
}
|
|
|
|
// This sample document uses structured document tags, which are not common
|
|
|
|
// except for in document templates. Normally you can just iterate over the
|
|
|
|
// document's paragraphs.
|
|
|
|
for _, sdt := range doc.StructuredDocumentTags() {
|
|
|
|
for _, p := range sdt.Paragraphs() {
|
|
|
|
paragraphs = append(paragraphs, p)
|
|
|
|
}
|
|
|
|
}
|
2025-02-19 18:30:19 +08:00
|
|
|
|
2025-02-25 18:36:33 +08:00
|
|
|
for _, p := range paragraphs {
|
|
|
|
for _, r := range p.Runs() {
|
|
|
|
if strings.Contains(r.Text(), "{{DATE_A}}") {
|
2025-02-26 18:58:43 +08:00
|
|
|
Replacetext(r, "{{DATE_A}}", time.Unix(tool.Timestamp("second"), 0).Format("2006年01月02日"))
|
2025-02-25 20:31:44 +08:00
|
|
|
} else if strings.Contains(r.Text(), "{{HLW_FW_1}}") { //插入互联网防火墙图片
|
|
|
|
Replaceimg(r, doc, "img/hw_fw1.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{HLW_AFC2000_1}}") { //插入互联网流量清洗图片
|
|
|
|
Replaceimg(r, doc, "img/afc2000_1.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{HLW_AFC2000_2}}") { //插入互联网流量清洗图片
|
|
|
|
Replaceimg(r, doc, "img/afc2000_2.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{HLW_TSGZ}}") { //插入互联网态势感知图片
|
|
|
|
Replaceimg(r, doc, "img/hw_tsgz.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{HLW_BLJ}}") { //插入互联网堡垒机图片
|
|
|
|
// Replaceimg(r, doc, "img/hw_fw1.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{ZWW_FW_1}}") { //插入政务网防火墙图片
|
|
|
|
Replaceimg(r, doc, "img/zww_fw1.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{ZWW_TSGZ}}") { //插入政务网态势感知图片
|
|
|
|
Replaceimg(r, doc, "img/zww_tsgz.png")
|
|
|
|
} else if strings.Contains(r.Text(), "{{ZWW_BLJ}}") { //插入政务网堡垒机图片
|
|
|
|
// Replaceimg(r, doc, "img/hw_fw1.png")
|
2025-02-25 18:36:33 +08:00
|
|
|
} else {
|
2025-02-25 20:31:44 +08:00
|
|
|
// log.Println("")
|
2025-02-25 18:36:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2025-02-25 20:31:44 +08:00
|
|
|
// 遍历文档中的表格,替换{{bianliang}} 中的内容
|
2025-02-25 18:36:33 +08:00
|
|
|
for _, table := range doc.Tables() {
|
|
|
|
for _, row := range table.Rows() {
|
2025-02-25 20:31:44 +08:00
|
|
|
for _, cell := range row.Cells() {
|
2025-02-25 18:36:33 +08:00
|
|
|
var (
|
|
|
|
b string
|
|
|
|
p document.Paragraph
|
|
|
|
k int
|
|
|
|
)
|
|
|
|
for k, p = range cell.Paragraphs() {
|
|
|
|
for _, a := range p.Runs() {
|
|
|
|
b += a.Text()
|
|
|
|
}
|
|
|
|
// log.Println(k)
|
|
|
|
}
|
2025-02-25 20:31:44 +08:00
|
|
|
// log.Println(i)
|
2025-02-25 18:36:33 +08:00
|
|
|
if strings.Contains(b, "{{DATE_B}}") {
|
2025-02-25 20:31:44 +08:00
|
|
|
log.Println("匹配到模板内容:", b)
|
2025-02-25 18:36:33 +08:00
|
|
|
for _, h := range cell.Paragraphs()[k].Runs() {
|
|
|
|
h.ClearContent()
|
|
|
|
}
|
|
|
|
cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
|
2025-02-25 20:31:44 +08:00
|
|
|
// log.Println(time.Date(2025, time.February, 25, 0, 0, 0, 0, time.Local).Format("2006年01月02日"))
|
2025-02-26 18:58:43 +08:00
|
|
|
cell.Paragraphs()[k].AddRun().AddText(time.Unix(tool.Timestamp("second"), 0).Format("2006年01月02日"))
|
2025-02-25 18:36:33 +08:00
|
|
|
}
|
2025-02-25 20:31:44 +08:00
|
|
|
// log.Println(b)
|
2025-02-25 18:36:33 +08:00
|
|
|
b = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-26 18:58:43 +08:00
|
|
|
doc.SaveToFile("大同信创云平台安全运维日报" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".docx")
|
2025-02-19 18:30:19 +08:00
|
|
|
}
|
2025-02-25 18:36:33 +08:00
|
|
|
|
|
|
|
// 文字替换
|
|
|
|
func Replacetext(r document.Run, src, ends string) {
|
|
|
|
datas := r.Text()
|
2025-02-25 20:31:44 +08:00
|
|
|
log.Println("匹配到的内容:", datas)
|
2025-02-25 18:36:33 +08:00
|
|
|
r.ClearContent()
|
|
|
|
r.AddText(strings.Replace(datas, src, ends, -1))
|
2025-02-25 20:31:44 +08:00
|
|
|
log.Println("替换后的内容:", strings.Replace(datas, src, ends, -1))
|
2025-02-19 18:30:19 +08:00
|
|
|
}
|
|
|
|
|
2025-02-25 20:31:44 +08:00
|
|
|
// 插入图片
|
|
|
|
func Replaceimg(r document.Run, doc *document.Document, path_img string) {
|
|
|
|
r.ClearContent()
|
2025-02-26 18:58:43 +08:00
|
|
|
img1, err := common.ImageFromFile(path_img)
|
2025-02-19 18:30:19 +08:00
|
|
|
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)
|
|
|
|
}
|
2025-02-25 20:31:44 +08:00
|
|
|
anchored, _ := r.AddDrawingInline(img1ref) //在文档中插入图片
|
|
|
|
anchored.SetSize(img1ref.RelativeWidth(5.2*measurement.Inch), 5.2*measurement.Inch)
|
|
|
|
log.Println("插入图片:", path_img)
|
2025-02-19 18:30:19 +08:00
|
|
|
}
|