dt_automate/wps/word_public.go

277 lines
7.9 KiB
Go
Raw Normal View History

2025-02-26 23:15:11 +08:00
package wps
import (
2025-03-14 18:20:23 +08:00
"fmt"
"image"
"image/jpeg"
"image/png"
2025-02-26 23:15:11 +08:00
"log"
2025-03-14 18:20:23 +08:00
"math"
"os"
"sort"
2025-02-26 23:15:11 +08:00
"strings"
"github.com/Esword618/unioffice/common"
"github.com/Esword618/unioffice/document"
"github.com/Esword618/unioffice/measurement"
2025-03-14 18:20:23 +08:00
"github.com/fogleman/gg"
2025-02-26 23:15:11 +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)
// }
// }
// 文字替换
func Replacetext(r document.Run, src, ends string) {
datas := r.Text()
log.Println("匹配到的内容:", datas)
r.ClearContent()
r.AddText(strings.Replace(datas, src, ends, -1))
log.Println("替换后的内容:", strings.Replace(datas, src, ends, -1))
}
// 插入图片
func Replaceimg(r document.Run, doc *document.Document, path_img string) {
r.ClearContent()
img1, err := common.ImageFromFile(path_img)
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)
}
anchored, _ := r.AddDrawingInline(img1ref) //在文档中插入图片
anchored.SetSize(img1ref.RelativeWidth(5.2*measurement.Inch), 5.2*measurement.Inch)
log.Println("插入图片:", path_img)
}
// 插入图片应用系统
func Replaceimg_1(r document.Run, doc *document.Document, path_img string) {
r.ClearContent()
img1, err := common.ImageFromFile(path_img)
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)
}
anchored, _ := r.AddDrawingInline(img1ref) //在文档中插入图片
anchored.SetSize(5.75*measurement.Inch, 3.2*measurement.Inch)
log.Println("插入图片:", path_img)
}
2025-03-14 18:20:23 +08:00
// 获取文档中的图片
func Weekly_get_img(file_img_path string) {
doc, err := document.Open(file_img_path)
if err != nil {
log.Panic("打开文件失败", err)
}
// 遍历文档中的所有关系,查找图片
// 遍历文档中的所有图片
// 遍历文档中的所有图片
for idx, img := range doc.Images {
// 获取图片的二进制数据
imgData := img.Path()
file, err := os.Open(imgData)
if err != nil {
log.Println("打开文件失败", err)
}
defer file.Close()
img, format, err := image.Decode(file)
if err != nil {
log.Println("解码图片失败", err)
}
// 生成图片文件名
imgFileName := fmt.Sprintf("image_%d.%s", idx, format)
// 打开文件以写入图片数据
outFile, err := os.Create("cache/" + imgFileName)
if err != nil {
log.Printf("无法创建文件 %s: %v", imgFileName, err)
continue
}
defer outFile.Close()
// 根据图片格式进行编码
switch format {
case "jpeg":
err = jpeg.Encode(outFile, img, nil)
case "png":
err = png.Encode(outFile, img)
default:
log.Printf("不支持的图片格式 %s", format)
continue
}
if err != nil {
log.Printf("无法保存图片 %s: %v", imgFileName, err)
} else {
log.Printf("图片 %s 已保存", "cace/"+imgFileName)
}
log.Println(imgData)
}
}
// 生成环形图表
func Weekly_chart() {
const (
width, height = 900, 700
centerX, centerY = width / 2, height / 2
radius = 180
)
// 初始化绘图上下文
dc := gg.NewContext(width, height)
dc.SetRGB(1, 1, 1) // 设置背景为白色
dc.Clear()
// 数据示例:每个部分的角度、标签和颜色
data := []struct {
angle float64
label string
color string
}{
{275, "1、敏感信息泄露275", "#FF6384"}, // 红色
{75, "2、代码执行75", "#36A2EB"}, // 蓝色
{68, "3、命令注入68", "#FFCE56"}, // 黄色
{47, "4、目录遍历47", "#4bc0c0"}, // 绿色
{28, "5、安全措施绕过28", "#c12c1f"}, // 红色
{12, "6、HTTP弱口令尝试12", "#779649"}, // 绿色
{10, "7、SQL注入10", "#9BA0C9"}, // 绿色
}
// 加载支持中文的字体
fontPath := "font/SIMFANG.TTF" // 确保字体文件在当前目录或指定路径下
fontSize := 16.0
if err := dc.LoadFontFace(fontPath, fontSize); err != nil {
panic(err)
}
// 绘制图表标题
dc.SetRGB(0, 0, 0)
dc.DrawString("安全事件类型分布Top10", float64(centerX-80), float64(centerY-250))
dc.DrawString("安全事件类型分布Top10", float64(centerX-400), float64(centerY-300))
// 绘制圆环图表
startAngle := 0.0
for _, d := range data {
endAngle := startAngle + d.angle
drawDonutSegment(dc, centerX, centerY, radius, startAngle, endAngle, d.color)
startAngle = endAngle
}
// 在右侧绘制数据标签
labelX := float64(centerX + radius + 50)
labelY := float64(centerY - (len(data)*20)/2)
for _, d := range data {
// 绘制颜色一致的小圆点
drawColorDot(dc, labelX, labelY-5, d.color)
// 绘制标签文本
dc.DrawString(d.label, labelX+15, labelY)
labelY += 20
}
// 保存为图片
dc.SavePNG("donut_chart_with_labels.png")
fmt.Println("圆环图表已保存为 donut_chart_with_labels.png")
}
// 绘制圆环的部分
func drawDonutSegment(dc *gg.Context, cx, cy, radius float64, startAngle, endAngle float64, color string) {
dc.SetHexColor(color)
dc.NewSubPath()
dc.DrawArc(cx, cy, radius, startAngle*math.Pi/180, endAngle*math.Pi/180)
dc.DrawArc(cx, cy, radius-60, endAngle*math.Pi/180, startAngle*math.Pi/180)
dc.ClosePath()
dc.Fill()
}
// 绘制颜色一致的小圆点
func drawColorDot(dc *gg.Context, x, y float64, color string) {
dc.SetHexColor(color)
dc.DrawCircle(x, y, 5)
dc.Fill()
}
// 绘制横向条形图
func DrawHorizontalBarChart() {
const (
width = 800
height = 400
barHeight = 20
margin = 130
)
// 初始化绘图上下文
dc := gg.NewContext(width, height)
dc.SetRGB(1, 1, 1) // 设置背景为白色
dc.Clear()
// 加载支持中文的字体
fontPath := "font/SIMFANG.TTF" // 确保字体文件在当前目录或指定路径下
fontSize := 16.0
if err := dc.LoadFontFace(fontPath, fontSize); err != nil {
panic(err)
}
// 绘制图表标题
dc.SetRGB(0, 0, 0)
dc.DrawStringAnchored("被外网攻击IP Top10", float64(width/2), 40, 0.5, 0)
// 定义数据和标签
data := []float64{100, 200, 150, 250, 300}
labels := []string{"11.2.144.18", "11.2.144.139", "11.2.144.156", "11.2.144.127", "11.2.144.129"}
// 创建一个索引数组
indices := make([]int, len(data))
for i := range indices {
indices[i] = i
}
// 对索引数组进行排序,排序依据是 data 数组中的值从大到小
sort.Slice(indices, func(i, j int) bool {
return data[indices[i]] > data[indices[j]]
})
// 根据排序后的索引重新排列 data 和 labels 数组
sortedData := make([]float64, len(data))
sortedLabels := make([]string, len(labels))
for i, idx := range indices {
sortedData[i] = data[idx]
sortedLabels[i] = labels[idx]
}
// 计算最大数据值
maxValue := 0.0
for _, value := range data {
if value > maxValue {
maxValue = value
}
}
// 绘制条形图
for i, value := range sortedData {
y := float64(margin + i*(barHeight+15))
barWidth := (value / maxValue) * float64(width-2*margin)
// 绘制条形
dc.SetRGB(0.2, 0.4, 0.8)
dc.DrawRectangle(float64(margin), y, barWidth, float64(barHeight))
dc.Fill()
// 绘制标签
dc.SetRGB(0, 0, 0)
dc.DrawStringAnchored(sortedLabels[i], float64(margin-10), y+barHeight/2, 1, 0.5)
// 绘制数值
dc.DrawStringAnchored(fmt.Sprintf("%d", int(value)), float64(margin+barWidth+10), y+barHeight/2, 0, 0.5)
}
// 保存为图片
dc.SavePNG("horizontal_bar_chart.png")
fmt.Println("横向条形图已保存为 horizontal_bar_chart.png")
}