使用评论

Aspose.Words 允许用户使用注释 - Aspose.Words 文档中的注释由 Comment 类表示。还可以使用 CommentRangeStartCommentRangeEnd 类来指定应与注释关联的文本区域。

添加评论

Aspose.Words 允许您通过多种方式添加注释:

1.使用Comment类 2.使用CommentRangeStartCommentRangeEnd

以下代码示例演示如何使用 Comment 类向段落添加注释:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.write("Some text is added.")
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
builder.current_paragraph.append_child(comment)
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_comments.docx")

以下代码示例演示如何使用文本区域以及 CommentRangeStartCommentRangeEnd 类向段落添加注释:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
para1 = aw.Paragraph(doc)
run1 = aw.Run(doc, "Some ")
run2 = aw.Run(doc, "text ")
para1.append_child(run1)
para1.append_child(run2)
doc.first_section.body.append_child(para1)
para2 = aw.Paragraph(doc)
run3 = aw.Run(doc, "is ")
run4 = aw.Run(doc, "added ")
para2.append_child(run3)
para2.append_child(run4)
doc.first_section.body.append_child(para2)
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
commentRangeStart = aw.CommentRangeStart(doc, comment.id)
commentRangeEnd = aw.CommentRangeEnd(doc, comment.id)
run1.parent_node.insert_after(commentRangeStart, run1)
run3.parent_node.insert_after(commentRangeEnd, run3)
commentRangeEnd.parent_node.insert_after(comment, commentRangeEnd)
doc.save(docs_base.artifacts_dir + "WorkingWithComments.anchor_comment.doc")

提取或删除评论

在 Word 文档中使用注释(除了跟踪更改之外)是审阅文档时的常见做法,特别是当有多个审阅者时。在某些情况下,您从文档中需要的唯一内容可能就是注释。假设您想要生成审核结果列表,或者您可能已经从文档中收集了所有有用的信息,并且您只想删除不必要的注释。您可能想要查看或删除特定审阅者的评论。

在此示例中,我们将了解一些简单的方法,用于从文档中的注释中收集信息以及从文档中删除注释。具体来说,我们将介绍如何:

  • 提取文档中的所有评论或仅提取特定作者的评论。
  • 删除文档中的所有评论或仅删除特定作者的评论。

如何提取或删除评论

该示例中的代码实际上非常简单,并且所有方法都基于相同的方法。 Word 文档中的注释由 Aspose.Words 文档对象模型中的 Comment 对象表示。要收集文档中的所有注释,请使用 get_child_nodes 方法,并将第一个参数设置为 NodeType.COMMENT。确保 获取子节点 方法的第二个参数设置为 true:这会强制 获取子节点 递归地从所有子节点中进行选择,而不是仅收集直接子节点。

为了说明如何从文档中提取和删除注释,我们将执行以下步骤:

1.使用Document类打开Word文档 1.将文档中的所有评论获取到一个集合中

  1. 提取评论:
    1. 使用 foreach 运算符遍历集合
    2. 提取并列出所有评论的作者姓名、日期和时间以及文本
    3. 提取并列出作者姓名、日期和时间以及特定作者撰写的评论文本,在本例中为作者"ks"
  2. 删除评论:
    1. 使用 for 运算符向后遍历集合 1.删除评论
  3. 保存更改

如何提取所有评论

get_child_nodes 方法非常有用,每次需要获取任何类型的文档节点列表时都可以使用它。生成的集合不会立即产生开销,因为仅当您枚举或访问其中的项目时,才会将节点选择到该集合中。

以下代码示例展示了如何提取文档中所有评论的作者姓名、日期和时间以及文本:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments(doc) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

如何提取指定作者的评论

Comment 节点选择到集合中后,您所要做的就是提取所需的信息。在此示例中,作者姓名首字母、日期、时间和评论的纯文本组合成一个字符串;您可以选择以其他方式存储它。

从特定作者提取评论的重载方法几乎相同,它只是在将信息添加到数组之前检查作者的姓名。

以下代码示例显示如何提取指定作者的评论的作者姓名、日期和时间以及文本:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments_by_author(doc, authorName) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
if (comment.author == authorName) :
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

如何删除评论

如果要删除所有评论,则无需在集合中逐一删除评论;您可以通过在评论集合上调用 clear 来删除它们。

以下代码示例展示了如何删除文档中的所有注释:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
comments.clear()

当您需要有选择地删除评论时,该过程变得与我们用于评论提取的代码更加相似。

以下代码示例展示了如何删除指定作者的评论:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments_by_author(doc, authorName) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
# Look through all comments and remove those written by the authorName.
for i in range(comments.count, 0) :
print(i)
comment = comments[i].as_comment()
if (comment.author == authorName) :
comment.remove()

这里要强调的要点是 for 运算符的使用。与简单的提取不同,这里要删除一条评论。一个合适的技巧是从最后一个 Comment 向后迭代集合到第一个 Comment。这样做的原因是,如果您从末尾开始向后移动,前面的项目的索引保持不变,并且您可以返回到集合中的第一项。

以下代码示例展示了注释提取和删除的方法:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
# Extract the information about the comments of all the authors.
for comment in self.extract_comments(doc) :
print(comment)
# Remove comments by the "pm" author.
self.remove_comments_by_author(doc, "pm")
print("Comments from \"pm\" are removed!")
# Extract the information about the comments of the "ks" author.
for comment in self.extract_comments_by_author(doc, "ks") :
print(comment)
# Read the comment's reply and resolve them.
self.comment_resolved_and_replies(doc)
# Remove all comments.
self.remove_comments(doc)
print("All comments are removed!")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.process_comments.docx")

如何删除 CommentRangeStart 和 CommentRangeEnd 之间的注释

使用 Aspose.Words,您还可以删除 CommentRangeStartCommentRangeEnd 节点之间的注释。

以下代码示例显示如何删除 CommentRangeStartCommentRangeEnd 之间的文本:

# Open the document.
doc = aw.Document(docs_base.my_dir + "Comments.docx")

commentStart = doc.get_child(aw.NodeType.COMMENT_RANGE_START, 0, True).as_comment_range_start()
commentEnd = doc.get_child(aw.NodeType.COMMENT_RANGE_END, 0, True).as_comment_range_end()

currentNode = commentStart
isRemoving = True
while (currentNode != None and isRemoving) :
    if (currentNode.node_type == aw.NodeType.COMMENT_RANGE_END) :
        isRemoving = False

    nextNode = currentNode.next_pre_order(doc)
    currentNode.remove()
    currentNode = nextNode

# Save the document.
doc.save(docs_base.artifacts_dir + "WorkingWithComments.remove_region_text.docx")

添加或删除评论的回复

add_reply 方法添加对此评论的回复。请注意,由于现有的 Microsoft Office 限制,文档中仅允许 1 级回复。如果在现有 Reply 注释上调用此方法,将引发 InvalidOperationException 类型的异常。

您可以使用remove_reply方法删除对此评论的指定回复。

以下代码示例展示了如何添加评论回复和删除评论回复:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
comment = doc.get_child(aw.NodeType.COMMENT, 0, True).as_comment()
comment.remove_reply(comment.replies[0])
comment.add_reply("John Doe", "JD", datetime(2017, 9, 25, 12, 15, 0), "New reply")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_remove_comment_reply.docx")

阅读评论的回复

replies 属性返回 Comment 对象的集合,这些对象是指定注释的直接子级。

以下代码示例展示了如何迭代评论的回复并解决它们:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def comment_resolved_and_replies(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
parentComment = comments[0].as_comment()
for child in parentComment.replies :
childComment = child.as_comment()
# Get comment parent and status.
print(childComment.ancestor.id)
print(childComment.done)
# And update comment Done mark.
childComment.done = True