Skip to content

Feat: Add low level comments and comment replies support #1467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

bhavasagar-dv
Copy link

@bhavasagar-dv bhavasagar-dv commented Jan 31, 2025

Feature: Enhanced Word Document Comments and comment replies Support

This pull request adds comprehensive support for comments and comment replies in Word documents, introducing new capabilities for handling comment metadata and references.

New Capabilities:

  1. Add comments to paragraphs.
  2. Creating threads with the existing comments.
  3. Spaning comments across multiple paragraphs.

Example Usage:

from docx import Document

document = Document("sample.docx")
p = document.add_paragraph("This is an example paragraph")
parent_comment = p.add_comment(
    "Testing Comment",
    author="Test Author",
    initials="TA",
    date="2025-01-01 00:00:00",
    resolved=True,
)
p.add_comment(
    "Testing Comment Reply 1",
    author="Test Author",
    initials="TA",
    date="2025-01-01 00:00:00",
    resolved=True,
    parent=parent_comment,
)
p.add_comment(
    "Testing Comment Reply 2",
    author="Test Author",
    initials="TA",
    date="2025-01-01 00:00:00",
    resolved=True,
    parent=parent_comment,
)
document.save("add_comments.docx")

To mark a comment over multiple paragraphs or just a single run

from docx import Document

document = Document("sample.docx")
para = document.add_paragraph()
comment = para.mark_comment_start("testing comment", "Test Author", "TA", "2025-01-01 00:00:00")
comment2 = para.mark_comment_start(
    "testing comment reply", "Test Author", "TA", "2025-01-01 00:00:00", parent=comment
)

para.add_run("Some random text")

para2 = document.add_paragraph("Another para")
para2.mark_comment_end(comment.id)
para2.mark_comment_end(comment2.id)
para2.add_run("Lorem ipsum dolor sit amet")

document.save("add_comments.docx")

Fixes the issue: #93

Another similar PR #624 but it doesn't address the comment replies. So, I have created this PR.

@bhavasagar-dv
Copy link
Author

@scanny can you please look into this, and if any changes are required please let us know, we are happy to contribute to this feature. Thanks.

…aphs.

Add `mark_comment_start` and `mark_comment_end` methods to support comments spanning across multiple paragraphs
@rdaim
Copy link

rdaim commented Feb 7, 2025

thanks,can you add a comment to a run? like this:

run = paragraph2.add_run('text1') #add a run to the paragraph

run.add_comment('comment') # add a comment only for the run text

run.add_comment('comment2')

run_comments = run.comments

@bhavasagar-dv
Copy link
Author

@rdaim we can add a comment for a run like this.

from docx import Document

document = Document("sample.docx")

para = document.add_paragraph()
comment = para.mark_comment_start("testing comment", "Test Author", "TA", "2025-01-01 00:00:00")
para.add_run("Some random text with a comment. ")
para.mark_comment_end(comment.id)
para.add_run("This text doesn't have a comment")

document.save("add_comments.docx")

@rdaim
Copy link

rdaim commented Feb 7, 2025

@rdaim we can add a comment for a run like this.

from docx import Document

document = Document("sample.docx")

para = document.add_paragraph()
comment = para.mark_comment_start("testing comment", "Test Author", "TA", "2025-01-01 00:00:00")
para.add_run("Some random text with a comment. ")
para.mark_comment_end(comment.id)
para.add_run("This text doesn't have a comment")

document.save("add_comments.docx")

wowww, thanks a lot!

@neilrathi01
Copy link

hi @bhavasagar-dv, What if a comment already exists and is linked to a certain paragraph, is there a way to reply to that comment using this?

@scanny
Copy link
Contributor

scanny commented Jun 13, 2025

Comment support is being added in #1495 if you want to take a look. Should merge in a week or so.

@scanny
Copy link
Contributor

scanny commented Jun 16, 2025

Comment support was added to python-docx in release 1.2.0 and is available on PyPI.

This feature includes a way to create comments (document.add_comment()) and to access the comments
that already exist (document.comments). Documentation for these features is available in the usual
place: https://python-docx.readthedocs.io/en/latest/user/comments.html

Note this support does not include the ability to directly access the comment "reference", i.e. the
range of text selected when the comment was created. It also does not include the threading or
resolution features of comments. These may follow in a later release, but are not currently
planned.

@bhavasagar-dv I'm closing this PR for now, but I see that you had implemented some of the comment features this did not include. The comments PR is here: #1495 if you want to take a look and perhaps adapt these features to the current support.

@scanny scanny closed this Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants