DEV Community

Allen Yang
Allen Yang

Posted on

Add and Manage Comments and Replies in Word Using Python

Python Working with Word Document Comments

In modern document collaboration scenarios, the commenting feature is an essential tool for team review and feedback. Whether it's code reviews, contract revisions, or academic paper modifications, you need to insert comments, reply to discussions, and extract comment content for organization within documents.

This article demonstrates how to perform the following operations in Word documents using Python:

  • Add comments to specific text
  • Reply to existing comments
  • Extract all comments from a document

Using the Spire.Doc for Python library, we can implement comment management in Word documents through Python code, making it suitable for batch processing document comments or automating document review workflows.

Environment Setup

First, install the Spire.Doc library:

pip install Spire.Doc
Enter fullscreen mode Exit fullscreen mode

This library provides a complete API for Word document operations, supporting features such as adding, modifying, replying to, and extracting comments.

Adding Comments to Specific Text

In practical applications, we often need to add annotations or notes to specific paragraphs or keywords in a document. The following example demonstrates how to find specified text and add comments to it.

Implementation Steps

  1. Load the Word document
  2. Find the target text
  3. Create comment markers (start and end)
  4. Set comment content and author information
  5. Insert the comment into the document
from spire.doc import *
from spire.doc.common import *

def add_comment_to_text(doc, keystring):
    """Add a comment to specified text"""
    # Find the target text
    find = doc.FindString(keystring, False, True)

    # Create comment markers
    commentmarkStart = CommentMark(doc)
    commentmarkStart.Type = CommentMarkType.CommentStart
    commentmarkStart.CommentId = 1

    commentmarkEnd = CommentMark(doc)
    commentmarkEnd.CommentId = 1
    commentmarkEnd.Type = CommentMarkType.CommentEnd

    # Set comment content
    comment = Comment(doc)
    comment.Format.CommentId = 1
    comment.Body.AddParagraph().Text = "This is a test comment"
    comment.Format.Author = "John Smith"

    # Get text range and locate paragraph
    range_list = find.GetRanges()
    para = range_list[0].OwnerParagraph

    # Get the index position of text in the paragraph
    index = para.ChildObjects.IndexOf(range_list[0])

    # Insert comment markers and comment content
    para.ChildObjects.Insert(index, commentmarkStart)
    para.ChildObjects.Insert(index + len(range_list) + 1, commentmarkEnd)
    para.ChildObjects.Add(comment)

# Load document
document = Document()
document.LoadFromFile("SampleDocument.docx")

# Add comment to target text
add_comment_to_text(document, "Important Clause")

# Save document
document.SaveToFile("AddComment.docx", FileFormat.Docx)
document.Close()
Enter fullscreen mode Exit fullscreen mode

Resulting document:

Python Adding Comments to Word Document

In the above code, CommentMark is used to identify the comment range, while the Comment object stores the specific content and metadata of the comment. The FindString method allows precise positioning of the text where comments need to be added.

Replying to Comments

When multiple people collaborate on document review, they need to reply to existing comments for discussion. Spire.Doc supports adding replies to comments, forming a complete discussion thread.

Implementation Method

from spire.doc import *
from spire.doc.common import *

# Load document containing comments
doc = Document()
doc.LoadFromFile("AddComment.docx")

# Get the first comment
original_comment = doc.Comments.get_Item(0)

# Create a reply comment
reply_comment = Comment(doc)
reply_comment.Format.Author = "Jane Doe"
reply_comment.Body.AddParagraph().AppendText(
    "I agree with this point. It would be beneficial to add more implementation details."
)

# Link the new comment as a reply to the original comment
original_comment.ReplyToComment(reply_comment)

# You can also add images to replies
docPicture = DocPicture(doc)
docPicture.LoadImage("AnnotationImage.png")
reply_comment.Body.Paragraphs[0].ChildObjects.Add(docPicture)

# Save document
doc.SaveToFile("AddReply.docx", FileFormat.Docx)
doc.Close()
Enter fullscreen mode Exit fullscreen mode

Execution result:

Python Adding Comment Replies to Word Document

Through the ReplyToComment method, newly created comments can be linked to existing comments, forming a hierarchical discussion structure. This is particularly useful in collaborative team reviews, enabling clear tracking of each issue's discussion process.

Extracting All Comments from a Document

When processing large volumes of documents, you may need to extract all comments for summary analysis or to generate review reports. The following code demonstrates how to iterate through and extract all comment content from a document.

Extraction Logic

from spire.doc import *
from spire.doc.common import *

# Load document
doc = Document()
doc.LoadFromFile("AddReply.docx")

content = ''

# Iterate through all comments
for i in range(doc.Comments.Count):
    comment = doc.Comments.get_Item(i)

    # Get comment author
    author = comment.Format.Author

    # Extract comment body content
    for j in range(comment.Body.Paragraphs.Count):
        paragraph = comment.Body.Paragraphs.get_Item(j)
        content += f"[Author: {author}]\n"
        content += paragraph.Text
        content += '\n\n'

# Save extracted content to a text file
with open("ExtractedComments.txt", "w", encoding="utf-8") as f:
    f.write(content)

print(f"Total comments extracted: {doc.Comments.Count}")
Enter fullscreen mode Exit fullscreen mode

Execution result:

Python Extracting All Comments from Word Document

This code iterates through the doc.Comments collection to access each comment object sequentially. For each comment, you can retrieve author information, comment content, and other metadata. The extracted content can be saved to a text file for subsequent processing or report generation.

Deleting and Replacing Comments

In certain scenarios, you may need to clean up outdated comments or update comment content. Spire.Doc provides functionality for deleting and replacing comments.

from spire.doc import *
from spire.doc.common import *

# Load document
doc = Document()
doc.LoadFromFile("AddReply.docx")

# Delete the second comment (reply content)
if doc.Comments.Count > 1:
    doc.Comments.RemoveAt(1)

# Or replace comment content
if doc.Comments.Count > 0:
    comment = doc.Comments.get_Item(0)
    # Clear existing content
    comment.Body.Paragraphs.Clear()
    # Add new content
    comment.Body.AddParagraph().Text = "Updated comment content"
    comment.Format.Author = "Robert Johnson"

# Save document
doc.SaveToFile("ProcessedDocument.docx", FileFormat.Docx)
doc.Close()
Enter fullscreen mode Exit fullscreen mode

Execution result:

Python Deleting and Replacing Comments in Word Document

Practical Application Scenarios

Comment management features are particularly useful in the following scenarios:

  • Batch Document Review: Automatically add standard comments to key clauses across multiple documents
  • Comment Summary Reports: Extract all comments to generate review summaries, allowing project managers to quickly understand issues
  • Version Comparison Support: Record change reasons and decision rationale through comments during document version iterations
  • Automated Workflows: Combine with other systems to automatically add or reply to comments based on specific rules

Important Considerations

When working with comments, keep the following points in mind:

  1. Comment ID Management: Ensure each comment's CommentId is unique to avoid conflicts
  2. Text Positioning Accuracy: When using FindString, consider case sensitivity and whole-word matching options
  3. Reply Hierarchy: Replies to comments create nested structures that require recursive processing during extraction
  4. Format Preservation: Comments can contain rich text and images; pay attention to format handling during extraction

Conclusion

This article has introduced the complete workflow for managing comments in Word documents using Python, including adding comments, replying to comments, extracting comment content, and deleting or replacing comments. Through these capabilities, you can automate document review processes and improve team collaboration efficiency.

The comment API provided by Spire.Doc for Python covers common document collaboration requirements. When combined with other document processing features, it enables the construction of comprehensive document automation solutions. In actual projects, you can combine these features according to specific needs to implement more complex document processing workflows.

Top comments (0)