Hi Team , Actually i struggling to add a comment to particular text in existing document. can anyone help with this?
using Aspose.words python.
@karthik3366 In .NET, Java and C++ version it is possible to achieve using IReplacingCallback
. Unfortunately, this feature is not yet available in Python version of Aspose.Words. The feature request is logged as WORDSNET-24685. We will keep you informed and let you know once it is resolved.
However, even if there is no elegant way to achieve this using IReplacingCallback
, you can workaround this with preprocessing the document using Ragnge.replace
method and then insert the comment. For example see the following code:
doc = aw.Document("C:\\Temp\\in.docx")
word = "test"
# Use Range.replace method to make each searched word a separate Run node.
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(word, "$0", opt)
# Get all runs
runs = doc.get_child_nodes(aw.NodeType.RUN, True)
for r in runs :
run = r.as_run()
# process the runs with text that matches the searched word.
if run.text == word:
# Crete a comment
comment = aw.Comment(doc, "James Bond", "007", datetime.date.today())
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
# Wrap the Run with CommentRangeStart and CommentRangeEnd
run.parent_node.insert_before(aw.CommentRangeStart(doc, comment.id), run)
run.parent_node.insert_after(aw.CommentRangeEnd(doc, comment.id), run)
# Add a comment.
run.parent_node.insert_after(comment, run)
doc.save("C:\\Temp\\out.docx")
It’s really helpful Thankyou!
Hi can you give me an update about when will be release the aspose.words python comment features…
@karthik3366 Comment feature is available in Aspose.Words for Python. What you are asking about is advanced find/replace functionality - an ability to use IReplacingCallback
. Unfortunately, this feature is not yet scheduled for implementation, so currently we cannot provide you any estimates.
At the moment, you can use the workaround I have suggested in my previous answer.