Skip to content
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

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

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")

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!

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.

2 participants