Documentation
¶
Overview ¶
读取word文档所需的库和函数
Index ¶
- type Docx
- func (r *Docx) AddBatchCommentToIndex(indices string, content string, auth string) (ids []string, failures map[int]error)
- func (r *Docx) AddBatchCommentToTable(indices string, commentText string, author string) (ids []string, failures map[int]error)
- func (r *Docx) AddBatchCommentToTableCell(cells []document.CellPos, commentText string, author string) ([]string, map[int]error)
- func (r *Docx) AddComment(index int, commentText string, author string) error
- func (r *Docx) AddCommentToIndex(pos int, content string, auth string) (*document.CommentOffsetResult, error)
- func (r *Docx) AddCommentToTable(pos int, commentText string, author string) (string, error)
- func (r *Docx) AddCommentToTableCell(tableIdx int, row int, col int, commentText string, author string) (string, error)
- func (r *Docx) AppendParagraph(text string) *document.Paragraph
- func (r *Docx) BatchSearchOnIndexParg(keywords []string) map[string][]int
- func (r *Docx) GetBody() *document.Body
- func (r *Docx) GetDoc() *document.Document
- func (r *Docx) GetElements() []any
- func (r *Docx) GetParaInfoAt(pos int) (*document.ParaInfo, error)
- func (r *Docx) GetParaInfoLen() (int, error)
- func (r *Docx) GetParaWithImages() []int
- func (r *Docx) GetStyle(val string) *style.Style
- func (r *Docx) GetTOCEntries() []document.TOCEntry
- func (r *Docx) GetTableInfoAt(pos int) (*document.TableInfo, error)
- func (r *Docx) GetTableInfoLen() (int, error)
- func (r *Docx) IndexParagraph()
- func (r *Docx) IndexTable()
- func (r *Docx) InsertParagraph(after int, text string) (*document.InsertOffsetResult, error)
- func (r *Docx) InsertTable(after int, table *document.Table) (*document.InsertOffsetResult, error)
- func (r *Docx) SaveImages(path string) error
- func (w *Docx) SaveToBytes() ([]byte, error)
- func (w *Docx) SaveToFile(path string) error
- func (w *Docx) SaveToUpload(remotePath string, upUrl string) (string, error)
- func (r *Docx) SearchEmptyOnIndexParg() []int
- func (r *Docx) SearchOnFirstParaIndex(text string) int
- func (r *Docx) SearchOnParaIndex(text string, start int, end int) []int
- func (r *Docx) SearchPageBreakOnIndexParg(lines int) []int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Docx ¶ added in v2.0.6
type Docx struct {
// contains filtered or unexported fields
}
func OpenDocxFromStream ¶ added in v2.0.6
func OpenDocxFromStream(rc io.ReadCloser) *Docx
OpenDocxFromStream 从流读取并打开文档
func (*Docx) AddBatchCommentToIndex ¶ added in v2.0.6
func (r *Docx) AddBatchCommentToIndex(indices string, content string, auth string) (ids []string, failures map[int]error)
BatchComment 对指定段落编号列表批量添加批注。 参数:
- indices 逗号分隔 "0,2 或范围 "2-4" 或者组合起来 "0,2-4"
- content 注释内容
- auth 注释作者
返回成功添加的批注 ID 列表和失败列表map。
-注意:body_paragraph 类型会按 BodyIndex 降序处理,避免插入 CommentRangeStart/End导致的索引偏移问题。cell_paragraph 类型不修改 Body.Elements,可任意顺序处理。
func (*Docx) AddBatchCommentToTable ¶ added in v2.0.9
func (r *Docx) AddBatchCommentToTable(indices string, commentText string, author string) (ids []string, failures map[int]error)
AddBatchCommentToTable 对指定表格编号列表批量添加批注。 indices 逗号分隔 "0,2" 或范围 "2-4" 或组合 "0,2-4"。 返回成功添加的批注 ID 列表和失败列表。
func (*Docx) AddBatchCommentToTableCell ¶ added in v2.1.1
func (r *Docx) AddBatchCommentToTableCell(cells []document.CellPos, commentText string, author string) ([]string, map[int]error)
AddBatchCommentToTableCell 对多个单元格批量添加批注。 cells 为单元格定位列表。返回成功添加的批注 ID 列表和失败列表(key 为 cells 下标)。
用法:
wd.IndexTable()
cells := []document.CellPos{{TableIndex: 0, Row: 0, Col: 1}, {TableIndex: 0, Row: 1, Col: 1}}
ids, fails := wd.AddBatchCommentToTableCell(cells, "需核对", "作者")
func (*Docx) AddComment ¶ added in v2.0.6
AddComment 对指定段落编号添加批注,能自动调整idx后续的编号实现。该方法可保持正确序号并连续调用
func (*Docx) AddCommentToIndex ¶ added in v2.0.6
func (r *Docx) AddCommentToIndex(pos int, content string, auth string) (*document.CommentOffsetResult, error)
AddCommentToIndex 对指定段落编号添加批注,并返回索引偏移报告。
返回的 (FromIdx, Offset):
- body_paragraph: (index+1, 2) — 段落编号 >= FromIdx 的 body_paragraph 需 BodyIndex+=2
- cell_paragraph: (0, 0) — 无影响,调用方可直接跳过
典型用法(手动逐段处理):
idx := doc.BuildParagraphIndex()
var offsets []*document.CommentOffsetResult
for _, i := range idx.Search("keyword") {
r, _ := doc.AddCommentToIndex(idx, i, "批注", "审核人")
if r.Offset > 0 {
offsets = append(offsets, r)
}
// 后续访问段落时,先调用 idx.ApplyOffsets(offsets) 修正 BodyIndex
}
func (*Docx) AddCommentToTable ¶ added in v2.0.9
AddCommentToTable 对指定序号的表格添加批注。 批注标记(CommentRangeStart/End + CommentReference)落在表格首单元格首段落。 pos 为表格在 IndexTable 生成的索引中的序号(0-based)。 返回批注 ID。
用法:
wd.IndexTable() id, err := wd.AddCommentToTable(0, "表格批注", "作者")
func (*Docx) AddCommentToTableCell ¶ added in v2.1.1
func (r *Docx) AddCommentToTableCell(tableIdx int, row int, col int, commentText string, author string) (string, error)
AddCommentToTableCell 对指定表格的指定单元格添加批注。 tableIdx 为表格在 IndexTable 生成的索引中的序号(0-based); row / col 为单元格行列下标(0-based)。 返回批注 ID。
批注引用落在目标单元格的第一个段落上;单元格为空时自动创建空段落。
用法:
wd.IndexTable() id, err := wd.AddCommentToTableCell(0, 1, 2, "该单元格数据有误", "作者")
func (*Docx) AppendParagraph ¶ added in v2.0.6
AppendParagraph(string) 文档最后追加段落
func (*Docx) BatchSearchOnIndexParg ¶ added in v2.0.6
SearchBatch 批量搜索多个关键词,返回 map[keyword] → 匹配段落编号列表。
func (*Docx) GetParaInfoAt ¶ added in v2.0.6
GetParaInfoAt 获取段落详细信息
func (*Docx) GetParaInfoLen ¶ added in v2.0.7
GetParaInfoLen 获取段落数量
func (*Docx) GetParaWithImages ¶ added in v2.0.6
GetParaWithImages 列出所有包含图片的段落,后续用法参考
totalImages := 0
for _, pi := range imageParas {
info := ¶Idx.Paras[pi]
totalImages += info.ImageCount
fmt.Printf(" 段落[%d] %s: %d 张图片\n", pi, info.Loc.Path, info.ImageCount)
for i, img := range info.ImageInfos {
fmt.Printf(" 图片 %d: name=%q type=%s relID=%s %dx%dpx\n",
i, img.Name, img.ImageType, img.RelationID, img.Width, img.Height)
}
}
func (*Docx) GetTOCEntries ¶ added in v2.0.8
GetTOCEntries 提取文档中所有目录条目。
基于 IndexParagraph 生成的段落索引实现,ParaIndex 与 IndexParagraph 编号一致。 若未调用 IndexParagraph,会自动构建索引。
具体提取逻辑见 toc.go 中的 getTOCEntries。
func (*Docx) GetTableInfoAt ¶ added in v2.0.8
GetTableInfoAt(int) 获得第几个表格,用法举例如下:
wd.IndexTable()
tbi, _ := wd.GetTableInfoAt(0)
if tbi != nil {
cellis, _ := tbi.Table.FindCellsByText("cell1", false)
for _, ci := range cellis {
fmt.Printf("匹配:[%d,%d]\n", ci.Row, ci.Col)
}
//读取指定行列的单元格文本
tbi.Table.GetCellText(0, 0)
}
func (*Docx) GetTableInfoLen ¶ added in v2.0.8
GetTableInfoLen() 获得表格总数量
func (*Docx) IndexParagraph ¶ added in v2.0.6
func (r *Docx) IndexParagraph()
IndexParagraph() 返回值已封装到内部,使用GetParaInfoAt获取段落详细信息,参考用法 返回值:idx ,列出所有段落。具体用法如下:
for i := 0; i < idx.Count(); i++ {
pi := idx.Paras[i]
//文字部分
text := pi.Text
//图片部分内容的获取方式
if pi.HasImage {
data, ext, err := idx.GetImageData(reader.GetDoc(), 100, 0)
if err == nil {
os.WriteFile("image_100_0."+ext, data, 0644)
}
}
}
func (*Docx) IndexTable ¶ added in v2.0.6
func (r *Docx) IndexTable()
IndexTable() 列出所有表格(包含嵌套的,深度优先算法)。idx已被隐藏列举对象用法如下:
for i := 0; i < idx.Count(); i++ {
ti := idx.GetTable(i)
text := ti.Text
parentInfo := ""
if ti.Depth > 0 {//是嵌套,有父表格,生成所在单元格位置
parentInfo = fmt.Sprintf(" parent=T[%d]@(%d,%d)", ti.ParentTableIndex, ti.ParentRow, ti.ParentCell)
}
fmt.Printf(" [%d] %s Depth=%d %dx%d%s => %q\n",i, ti.Path, ti.Depth, ti.RowCount, ti.ColCount, parentInfo, text)
}
func (*Docx) InsertParagraph ¶ added in v2.0.6
InsertParagraph(string, int) 在指定段落编号之后插入一个新段落。 参数:
- idx: 已构建的段落索引快照 调用GetAllParagraphs()获得
- afterIndex: 在此段落编号之后插入(-1 表示在文档开头插入)
- text: 新段落的文本内容
返回: InsertOffsetResult,调用方应在插入后调用 idx.Rebuild(doc) 重建整个索引。
- type InsertOffsetResult struct {
- NewPara *Paragraph // 新插入的段落对象
- NewParaIdx int // 新段落的编号
- FromIdx int // 从此编号开始,所有后续段落编号需 +Offset
- Offset int // 偏移量(每插入一个段落 = +1)
- ShiftStart int // body_paragraph 的 BodyIndex 偏移起始位置
func (*Docx) InsertTable ¶ added in v2.0.6
InsertCell 在指定段落编号之后插入一个表格。 参数:
- idx: 已构建的段落索引快照
- after: 在此段落编号之后插入(-1 表示在文档开头插入)
- table: 要插入的表格对象(可通过 CreateTable 创建)
返回: InsertOffsetResult,调用方应在插入后调用 idx.Rebuild(doc) 重建索引。
- type InsertOffsetResult struct {
- NewPara *Paragraph // 新插入的段落对象 因为是表格返回值为nil
- NewParaIdx int // 新段落的编号
- FromIdx int // 从此编号开始,所有后续段落编号需 +Offset
- Offset int // 偏移量(每插入一个段落 = +1)
- ShiftStart int // body_paragraph 的 BodyIndex 偏移起始位置
func (*Docx) SaveImages ¶ added in v2.0.6
SaveImages 批量保存素有图片到本地目录
func (*Docx) SaveToBytes ¶ added in v2.0.6
func (*Docx) SaveToFile ¶ added in v2.0.6
func (*Docx) SaveToUpload ¶ added in v2.0.6
func (*Docx) SearchEmptyOnIndexParg ¶ added in v2.0.6
SearchEmpty 定位索引中所有空白段落,返回匹配的段落编号列表。 空白段落定义:段落文本为空,或仅包含空白字符。 空白字符包括:ASCII 空格、中文全角空格(\u3000)、制表符、换行等。
func (*Docx) SearchOnFirstParaIndex ¶ added in v2.0.8
SearchOnFirstParaIndex 在索引中搜索包含指定子串的段落,返回匹配的第一个段落编号。 start/end 指定段落编号范围(含两端),start<0 视为 0,end<0 视为末尾。未找到返回 -1。
func (*Docx) SearchOnParaIndex ¶ added in v2.0.8
SearchOnParaIndex 在索引中搜索包含指定子串的段落,返回匹配的段落编号列表。 start/end 指定段落编号范围(含两端),start<0 视为 0,end<0 视为末尾。这是纯内存切片扫描,无树遍历开销。
func (*Docx) SearchPageBreakOnIndexParg ¶ added in v2.0.6
SearchPageBreak 定位索引中所有包含换页符号的段落,返回匹配的段落编号列表。 换页符来源包括两类:
- Run 中的 w:br 元素 type="page"(显式分页符)
- 段落属性 w:pageBreakBefore(段前分页)
如果 lines = 0 不考虑后续逻辑 如果 lines > 0,则同时将连续 lines 个空白段落视为一个分页: 扫描索引,每发现 lines 个连续空白段落,将其中第一个段落的编号加入结果。 正常分页与空白分页的去重结果按编号升序返回。