DEV Community

Allen Yang
Allen Yang

Posted on

Add and Manage Comments in PowerPoint with Python

Add and Manage Comments in PowerPoint with Python

In team collaboration and presentation review scenarios, the comment feature plays a critical role. Reviewers add comments to slides to propose changes, flag questions, or record feedback, while presentation authors need to read, respond to, and manage those comments. When dealing with large numbers of slides or batch review processes, managing comments manually is both inefficient and error-prone. This article provides a detailed guide on how to add, extract, modify, and delete comments in PowerPoint presentations programmatically with Python, enabling fully automated comment management.

Why Manage Comments Programmatically

Compared with manually operating comments in the PowerPoint application, a programmatic approach offers the following advantages:

  • Batch review: Extract comments from all slides in a single pass to generate review reports
  • Automated workflows: Integrate comment management into CI/CD pipelines or document processing workflows
  • Precise control: Position comments using coordinates to ensure accurate annotation placement
  • Collaboration integration: Export comment data to project management tools or feedback tracking systems

Environment Setup

Before starting, install the Python library that supports PowerPoint operations. This article uses Spire.Presentation for Python, which provides a comprehensive API for manipulating PPTX-format presentations.

pip install Spire.Presentation
Enter fullscreen mode Exit fullscreen mode

Once installed, import the relevant modules in your script to begin working.

Adding Comments

Adding comments is the foundational operation of comment management. In PowerPoint, each comment is associated with an author and a specific slide position. The following code demonstrates how to add a comment to a slide:

from spire.presentation.common import *
from spire.presentation import *

inputFile = "./Data/AddComment.pptx"
outputFile = "AddComment.pptx"

# Create a Presentation object and load the file
presentation = Presentation()
presentation.LoadFromFile(inputFile)

# Add a comment author
author = presentation.CommentAuthors.AddAuthor("E-iceblue", "comment:")

# Set the comment position coordinates
point = PointF.Empty()
point.X = 18
point.Y = 25

# Add a comment to the first slide
presentation.Slides[0].AddComment(
    author, "This is a comment", point, DateTime.get_Now())

# Save the document
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

Key steps in the code:

  1. Create an author: The AddAuthor() method accepts two parameters — the author name and the author initials (typically used as an identifier prefix in comments). Each comment must be associated with a registered author.
  2. Position the comment: The PointF object specifies the anchor position of the comment on the slide. The X and Y properties represent the horizontal and vertical coordinates, respectively.
  3. Add the comment: The AddComment() method accepts four parameters — the author object, the comment text, the position coordinates, and the posting time. DateTime.get_Now() sets the current time as the comment timestamp.

Extracting Slide Comments

In review workflows, extracting comment information is a critical step for generating review reports. The following code shows how to extract all comments from a specific slide:

from spire.presentation.common import *
from spire.presentation import *

def AppendAllText(fname: str, text: List[str]):
    fp = open(fname, "w")
    for s in text:
        fp.write(s + "\n")
    fp.close()

inputFile = "./Data/Template_Ppt_5.pptx"
outputFile = "ExtractComments.txt"

# Create a Presentation object and load the file
presentation = Presentation()
presentation.LoadFromFile(inputFile)

cs = []

# Get all comments from the first slide
comments = presentation.Slides[0].Comments

# Iterate through comments and collect text
i = 0
while i < len(comments):
    cs.append(comments[i].Text + "\r\n")
    i += 1

# Save to a text file
AppendAllText(outputFile, cs)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

Slides[0].Comments returns the collection of all comments on the first slide. By iterating through this collection and accessing the Text property of each comment, you can retrieve all comment text content.

Getting Detailed Comment Information

Beyond the comment text, complete comment information includes the author name and posting time. By iterating through the CommentAuthors collection, you can obtain richer comment metadata:

from spire.presentation.common import *
from spire.presentation import *

def AppendAllText(fname: str, text: List[str]):
    fp = open(fname, "w")
    for s in text:
        fp.write(s + "\n")
    fp.close()

inputFile = "./Data/Comments.pptx"
outputFile = "GetSlideComments.txt"

# Create a Presentation object and load the file
presentation = Presentation()
cs = []
presentation.LoadFromFile(inputFile)

# Iterate through all comment authors
for commentAuthor in presentation.CommentAuthors:
    # Iterate through each author's comments
    for comment in commentAuthor.CommentsList:
        # Get comment information
        commentText = comment.Text
        authorName = comment.AuthorName
        time = comment.DateTime
        cs.append("Comment text: " + commentText + "\n" + "Comment author: " +
                  authorName + "\n" + "Posted on: " + time.ToString())

AppendAllText(outputFile, cs)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

This code retrieves complete comment information through a two-level iteration:

  • The outer loop iterates through the CommentAuthors collection to access each comment author
  • The inner loop iterates through the author's CommentsList to extract the text, author name, and timestamp of each comment

This approach is particularly useful for generating structured review reports with comments grouped by author.

Modifying and Deleting Comments

During the review process, it is often necessary to modify comment content or remove comments that are no longer needed. The following code demonstrates both modification and deletion operations:

from spire.presentation.common import *
from spire.presentation import *

inputFile = "./Data/DeleteComment.pptx"
outputFile = "DeleteComment.pptx"

# Create a Presentation object and load the file
presentation = Presentation()
presentation.LoadFromFile(inputFile)

# Modify the text of the second comment
presentation.Slides[0].Comments[1].Text = "Updated comment text"

# Delete the third comment
presentation.Slides[0].DeleteComment(presentation.Slides[0].Comments[2])

# Save the document
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

Core explanations for both operations:

  • Modifying a comment: Assign a new value directly to the Comments[index].Text property to update the comment text. This approach is suitable for correcting erroneous descriptions or adding supplementary information.
  • Deleting a comment: The DeleteComment() method accepts a comment object as a parameter and permanently removes it from the slide. You must first access the comment object through the Comments collection by index, then pass it to the method for deletion.

Practical Tips

Batch Extracting Comments from All Slides

In real-world review scenarios, you often need to extract comments from all slides in the presentation. This can be achieved by iterating through the slides collection:

all_comments = []
for i in range(presentation.Slides.Count):
    slide = presentation.Slides[i]
    for j in range(len(slide.Comments)):
        comment = slide.Comments[j]
        all_comments.append({
            "slide": i + 1,
            "text": comment.Text,
            "author": comment.AuthorName
        })
Enter fullscreen mode Exit fullscreen mode

After collecting comment data into a list, you can further export it to CSV, JSON, or directly generate a review report.

Filtering Comments by Author

When multiple reviewers add comments simultaneously, filtering by author helps quickly locate feedback from a specific reviewer:

target_author = "E-iceblue"
for commentAuthor in presentation.CommentAuthors:
    if commentAuthor.Name == target_author:
        for comment in commentAuthor.CommentsList:
            print(f"Comment: {comment.Text}  Time: {comment.DateTime}")
Enter fullscreen mode Exit fullscreen mode

Conclusion

This article provided a detailed walkthrough of the complete workflow for managing comments in PowerPoint presentations with Python, covering the four core operations: adding, extracting, modifying, and deleting comments.

Key takeaways:

  1. Use CommentAuthors.AddAuthor() to register comment authors and Slides[index].AddComment() to add comments
  2. Access and extract comments on a slide through the Slides[index].Comments collection
  3. Iterate through the CommentAuthors collection to obtain complete comment information including author names and timestamps
  4. Modify comment text via the Comments[index].Text property and remove comments using the DeleteComment() method

With these skills, you can integrate comment management into automated review workflows, enabling batch extraction, filtering, and report generation for comments, significantly improving team collaboration efficiency.

Top comments (0)