package wps import ( "dt_automate/tool" "log" "strings" "time" "github.com/Esword618/unioffice/document" "github.com/Esword618/unioffice/schema/soo/wml" ) func SAFET_Word() { doc, err := document.Open("temp/temp.docx") if err != nil { log.Panic("打开文件失败", err) } // defer doc.Close() // 填充模板中的变量 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) } } log.Println("开始生成安全巡检模板") for _, p := range paragraphs { for _, r := range p.Runs() { if strings.Contains(r.Text(), "{{DATE_A}}") { Replacetext(r, "{{DATE_A}}", time.Unix(tool.Timestamp("second"), 0).Format("2006年01月02日")) } 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_baolj.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/zww_baolj.png") } else { // log.Println("") } } } // 遍历文档中的表格,替换{{bianliang}} 中的内容 for _, table := range doc.Tables() { for _, row := range table.Rows() { for _, cell := range row.Cells() { 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) } // log.Println(i) if strings.Contains(b, "{{DATE_B}}") { log.Println("匹配到模板内容:", b) for _, h := range cell.Paragraphs()[k].Runs() { h.ClearContent() } cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter) // log.Println(time.Date(2025, time.February, 25, 0, 0, 0, 0, time.Local).Format("2006年01月02日")) cell.Paragraphs()[k].AddRun().AddText(time.Unix(tool.Timestamp("second"), 0).Format("2006年01月02日")) } // log.Println(b) b = "" } } } doc.SaveToFile("大同信创云平台安全运维日报" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".docx") }