165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
package wps
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"strings"
|
|
|
|
"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"
|
|
)
|
|
|
|
// 设置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)
|
|
}
|
|
}
|
|
|
|
func Word() {
|
|
doc, err := document.Open("temp.docx")
|
|
if err != nil {
|
|
log.Println("打开文件失败", err)
|
|
}
|
|
defer doc.Close()
|
|
// 填充模板中的变量
|
|
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)
|
|
}
|
|
}
|
|
|
|
for _, p := range paragraphs {
|
|
for _, r := range p.Runs() {
|
|
if strings.Contains(r.Text(), "{{DATE_A}}") {
|
|
Replacetext(r, "{{DATE_A}}", "2025年02月25日")
|
|
} else if strings.Contains(r.Text(), "{{DATE_A}}") {
|
|
// r.ClearContent()
|
|
} else {
|
|
// fmt.Println("not modifying", r.Text())
|
|
}
|
|
|
|
}
|
|
}
|
|
// 遍历文档中的表格
|
|
for _, table := range doc.Tables() {
|
|
for _, row := range table.Rows() {
|
|
for i, 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(k)
|
|
for _, h := range cell.Paragraphs()[k].Runs() {
|
|
h.ClearContent()
|
|
}
|
|
cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
|
|
cell.Paragraphs()[k].AddRun().AddText("2025年02月25日")
|
|
}
|
|
log.Println(b)
|
|
b = ""
|
|
}
|
|
}
|
|
}
|
|
doc.SaveToFile("simple.docx")
|
|
}
|
|
|
|
// 文字替换
|
|
func Replacetext(r document.Run, src, ends string) {
|
|
datas := r.Text()
|
|
r.ClearContent()
|
|
r.AddText(strings.Replace(datas, src, ends, -1))
|
|
log.Println(strings.Replace(datas, src, ends, -1))
|
|
}
|
|
|
|
// func createParaRun(doc *document.Document, s string) document.Run {
|
|
// para := doc.AddParagraph()
|
|
// run := para.AddRun()
|
|
// run.AddText(s)
|
|
// return run
|
|
// }
|
|
|
|
var lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`
|
|
|
|
func Word_img() {
|
|
doc := document.New()
|
|
|
|
img1, err := common.ImageFromFile("gophercolor.png")
|
|
if err != nil {
|
|
log.Fatalf("unable to create image: %s", err)
|
|
}
|
|
img2data, err := ioutil.ReadFile("gophercolor.png")
|
|
if err != nil {
|
|
log.Fatalf("unable to read file: %s", err)
|
|
}
|
|
img2, err := common.ImageFromBytes(img2data)
|
|
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)
|
|
}
|
|
img2ref, err := doc.AddImage(img2)
|
|
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("Gopher")
|
|
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) //设置文本环绕正方形
|
|
|
|
run := para.AddRun()
|
|
for i := 0; i < 16; i++ {
|
|
run.AddText(lorem)
|
|
|
|
// drop an inline image in
|
|
if i == 13 {
|
|
inl, err := run.AddDrawingInline(img1ref)
|
|
if err != nil {
|
|
log.Fatalf("unable to add inline image: %s", err)
|
|
}
|
|
inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
|
|
}
|
|
if i == 15 {
|
|
inl, err := run.AddDrawingInline(img2ref)
|
|
if err != nil {
|
|
log.Fatalf("unable to add inline image: %s", err)
|
|
}
|
|
inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
|
|
}
|
|
}
|
|
doc.SaveToFile("image.docx")
|
|
}
|