# go-wordControl **Repository Path**: netbycom/go-wordControl ## Basic Information - **Project Name**: go-wordControl - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-19 - **Last Updated**: 2025-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # WordControl [![Go Version](https://img.shields.io/badge/Go-1.16+-blue.svg)](https://golang.org) [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) 一个强大的 Go 语言 Word 文档处理库,支持创建、编辑和解析 Word 文档。提供链式调用 API,操作简单直观。 ## ✨ 特性 - 🚀 **简单易用** - 链式调用 API,代码简洁优雅 - 📝 **完整功能** - 支持段落、表格、图片、链接等 Word 元素 - 🎨 **样式丰富** - 支持字体、颜色、大小、对齐等样式设置 - 📋 **表格支持** - 创建复杂表格,支持边框、合并单元格等 - 🖼️ **图片处理** - 支持插入和管理图片 - 🔗 **超链接** - 创建和编辑超链接 - 📖 **文档解析** - 解析现有 Word 文档并提取内容 - 🔄 **模板系统** - 支持段落和表格模板,批量生成内容 - 🛠️ **文档修改** - 编辑现有 Word 文档内容 ## 📦 安装 ```bash go get gitee.com/netbycom/wordControl ``` ## 🚀 快速开始 ### 基础文档创建 ```go package main import ( "gitee.com/netbycom/wordControl/easyWord" ) func main() { // 创建 Word 控制器 word := easyWord.NewWordControl() // 创建标题段落 word.CreateSection(func(section *easyWord.WordSection) { section.Align("center") section.CreateText(func(text *easyWord.WordText) { text.Text("文档标题"). Size(18). Font("微软雅黑"). Bold(). Color("#333333") }) }) // 创建正文段落 word.CreateSection(func(section *easyWord.WordSection) { section.Align("left") section.CreateText(func(text *easyWord.WordText) { text.Text("这是一段正文内容,支持丰富的样式设置。"). Size(12). Font("宋体"). Spacing(1) }) }) // 生成文档 word.CreateXML().Save("document.docx") } ``` ### 表格创建 ```go // 创建表格 word.CreateTable(func(table *easyWord.WordTable) { // 设置表格边框 table.Borders(func(borders *easyWord.WordTableBorders) { borders.Left().Size(1).Color("#000000") borders.Right().Size(1).Color("#000000") borders.Top().Size(1).Color("#000000") borders.Bottom().Size(1).Color("#000000") }) // 创建表头行 table.CreateRow(func(row *easyWord.WordTableRow) { row.CreateCell(func(cell *easyWord.WordTableCell) { cell.Width(2000) cell.CreateSection(func(section *easyWord.WordSection) { section.CreateText(func(text *easyWord.WordText) { text.Text("姓名").Bold() }) }) }) row.CreateCell(func(cell *easyWord.WordTableCell) { cell.Width(2000) cell.CreateSection(func(section *easyWord.WordSection) { section.CreateText(func(text *easyWord.WordText) { text.Text("年龄").Bold() }) }) }) }) // 创建数据行 table.CreateRow(func(row *easyWord.WordTableRow) { row.CreateCell(func(cell *easyWord.WordTableCell) { cell.CreateSection(func(section *easyWord.WordSection) { section.CreateText(func(text *easyWord.WordText) { text.Text("张三") }) }) }) row.CreateCell(func(cell *easyWord.WordTableCell) { cell.CreateSection(func(section *easyWord.WordSection) { section.CreateText(func(text *easyWord.WordText) { text.Text("25") }) }) }) }) }) ``` ### 模板使用 ```go // 创建段落模板 word.CreateSection(func(section *easyWord.WordSection) { section.Align("left") section.CreateText(func(text *easyWord.WordText) { text.Text("默认内容").Size(12).Font("宋体") }) }).CreateTemplate("content") // 使用模板生成内容 word.TemplateText("content", "这是通过模板生成的具体内容") ``` ## 📖 API 文档 ### WordControl 主要方法 | 方法 | 描述 | |------|------| | `NewWordControl()` | 创建新的 Word 控制器 | | `CreateSection(func)` | 创建段落 | | `CreateTable(func)` | 创建表格 | | `Load(filename)` | 加载现有 Word 文档 | | `CreateXML()` | 生成 XML 文档 | | `Save(filename)` | 保存文档 | | `GetFontSize(name)` | 获取预定义字体大小 | ### WordSection 段落方法 | 方法 | 描述 | |------|------| | `Align(align)` | 设置对齐方式 (left/center/right) | | `Spacing(spacing)` | 设置行间距 | | `Ind(ind)` | 设置缩进 | | `CreateText(func)` | 创建文本 | | `CreateLink(func)` | 创建超链接 | | `SetStyle(style)` | 设置样式 | ### WordText 文本方法 | 方法 | 描述 | |------|------| | `Text(text)` | 设置文本内容 | | `Size(size)` | 设置字体大小 | | `Font(font)` | 设置字体 | | `Color(color)` | 设置颜色 (支持十六进制) | | `Bold()` | 设置粗体 | | `Italic()` | 设置斜体 | | `Underline()` | 设置下划线 | | `Spacing(spacing)` | 设置字间距 | ### WordTable 表格方法 | 方法 | 描述 | |------|------| | `CreateRow(func)` | 创建行 | | `Borders(func)` | 设置边框样式 | | `Width(width)` | 设置表格宽度 | | `CreateTemplate(name)` | 创建表格模板 | | `TemplateText(name, data)` | 使用模板生成数据 | ## 🔧 高级功能 ### 文档解析 ```go // 解析现有文档 word := easyWord.NewWordControl() sections := word.Load("input.docx").ParseWord() // 获取所有段落文本 texts := sections.GetWordSections() fmt.Println(texts) // 获取段落对象列表 sectionList := sections.GetWordSectionLists() // 修改特定段落 section := sections.GetWordSectionItem(0)["section"] textObj := section.GetTextsObj()[0] textObj.Text("新的文本内容") section.UpdateText(textObj) // 替换文本 section.ReplaceText("旧文本", "新文本") ``` ### 样式管理 ```go // 创建自定义样式 style := easyWord.NewWordStyle() style.Size(16).Font("宋体").Color("#333333").Spacing(1) // 应用样式 text.Style(style) ``` ## 📋 示例 查看 `examples/` 目录获取更多完整示例: - `main.go` - 基础文档创建示例 - `main_parser.go` - 文档解析示例 ## 🤝 贡献 欢迎提交 Issue 和 Pull Request! 1. Fork 本仓库 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 创建 Pull Request ## 📄 许可证 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。 ## 🔗 相关链接 - [Go 语言官网](https://golang.org/) - [Word XML 格式规范](https://schemas.openxmlformats.org/wordprocessingml/2006/main/) --- **WordControl** - 让 Word 文档处理变得简单高效!