- Code: Select all
# 查找要添加评论的文本
text = doc.FindString("交换媒介", True, True)
# 创建一个评论对象并设置评论的内容和作者
comment = Comment(doc)
comment.Body.AddParagraph().Text = "交换媒介还包括商品交换、信贷和资产交换等。"
comment.Format.Author = "琳达"
# 将找到的文本作为文本范围,并获取其所属的段落
range = text.GetAsOneRange()
paragraph = range.OwnerParagraph
# 将评论添加到段落中
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(range) + 1, comment)
# 创建评论起始标记和结束标记,并将它们设置为创建的评论的起始标记和结束标记
commentStart = CommentMark(doc, CommentMarkType.CommentStart)
commentEnd = CommentMark(doc, CommentMarkType.CommentEnd)
commentStart.CommentId = comment.Format.CommentId
commentEnd.CommentId = comment.Format.CommentId
# 在找到的文本之前和之后插入创建的评论起始和结束标记
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(range), commentStart)
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(range) + 1, commentEnd)
# 保存文档
doc.SaveToFile("output/批注文本.docx")
doc.Close()
above is a code of create a comment of a key word,but the negative influence is that if i have a batch of different key words to comment, i should traverse the whole word docment time and time again.once can handle one key word.
if i want to handle a list of key words in one line , this would be a low performance way.
the best way i can suppose is that i can traverse the whole doc only a time , and then handle every line to find the range of the key word,the same time, i can create a new comment of the key word.
but the problem is that ,there is not apis for a Paragraph object to query the range of key word . like Document‘s method findString or findAllStrings .
How can i do to achieve my program