GO-unioffice实现word编辑

导包

Go 复制代码
import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/unidoc/unioffice/common/license"
	"github.com/unidoc/unioffice/document"
)

创建word文件

Go 复制代码
func CreateFile(name string) {
	filename := name + ".docx"
	fmt.Println(filename)
	var filepath = "./"
	err := os.MkdirAll(filepath, 0777)
	if err != nil {
		panic(err)
	}
	var path = filepath + filename
	_, err = os.Create(path)
	if err != nil {
		panic(err)
	} else {
		log.Println("创建《", filename, "》文件成功")
	}
}

获取unicloud密钥

UniCLOUDhttps://cloud.unidoc.io/#/api-keys通过上面的网站注册获取密钥,这会提供一个免费的密钥。

编辑word

我的天,os创建的docx文档,他竟然识别不了,会报错

Go 复制代码
doc, err := document.Open(path)
	if err != nil {
		log.Fatalf("error opening document: %s", err)

	}

创建《 cs.docx 》文件成功

2023/10/15 23:29:42 error opening document: parsing zip: zip: not a valid zip file

改成wps创建的docx试试。

也是一样报错,但是输入几个字后,重新执行,运行正常了

相关推荐
techdashen6 小时前
Go Map 详解:键值对实际上是如何存储的
开发语言·后端·golang
ttwuai10 小时前
Go 后台初始化 MySQL 脚本失败怎么办?先查 DDL 还是生成配置
开发语言·mysql·golang
名字还没想好☜11 小时前
Go 的 TCP 粘包与拆包:用长度前缀协议 + bufio 正确读消息
后端·tcp/ip·golang·go·php
王的宝库1 天前
Go 项目结构:从单文件到标准工程布局
开发语言·后端·golang
花酒锄作田1 天前
Go - Gin中使用sessions
golang
不甘先生1 天前
Go 中 type、方法与指针接收者:从 str_name.Name() 看懂 Go 的类型系统
开发语言·后端·golang
Casbin开源社区1 天前
一个面板管住 29 个 AI 编程 Agent:Casbin Gateway 的配置统一、协议互转、用量统计与权限管控
人工智能·golang·开源·gateway·casbin
名字还没想好☜1 天前
Go 标准库 flag 包实战:参数解析、子命令、自定义 Value 类型与默认值
开发语言·后端·golang·go
古少侠1 天前
用 DS随心转 把 AI 画的 Mermaid 流程图转成 Word 里的高清图
开发语言·c#·word
zh73141 天前
Go 1.23 → 1.26 升级检查文档
开发语言·chrome·golang