Highlight and add comment to text

I am not able to identify it, using istitle() only description is identifying but not project description. But I want to identify both description and project description

@harshitha112000, Could you please share the code example how you try to identify Description as Heading 1 and Project description as Heading2 using the istitle() function?

No I didn’t Identify it using istitle() as heading1 and 2. I am just asking how to find project decsription from the document because istitle() is only identifying description but not project description

@harshitha112000, sorry, I cannot analyze your problem without seeing your code. Is the istitle() function the function you wrote?

runs = doc.get_child_nodes(aw.NodeType.RUN, True)
    
for r in runs :
    run = r.as_run()
    if run.text == word:
        if run.parent_paragraph.get_text().istitle():
            run.font.highlight_color = ad.Color.light_coral

This code is identifying only description but not project description

@harshitha112000, the istitle() function is the built-in Python function. It is not a part of Aspose.Words API. You cannot use this function for reliable detection of headings in Word documents, as this function only checks if the string is a titlecased string.

You should apply one of the heading styles to the heading in Word document, and then you can use is_heading property as shown in the code example above.

ok, but using aspose is there any way to identify these texts which are like headings? as is_heading() is not working because it’s not of heading style

@harshitha112000, could you please define how you would detect whether the text is a heading? Is it some specific font, font size, font style or something else?

You could add checks in the code for these properties of run.font, for example.

If it is bold maybe

@harshitha112000, the paragraphs that you consider as headings are list items. You could use is_list_item for distinguishing between heading paragraphs and regular text paragraphs in addition to font boldness and font size:

doc = Document("Document.docx")

for node in doc.get_child_nodes(NodeType.PARAGRAPH, True):
    para = node.as_paragraph()
    if para.is_list_item:
        first_run = para.first_child.as_run()
        font_size = first_run.font.size
        is_bold = first_run.font.bold
        if is_bold:
            if font_size == 14:
                print("Heading 1:", para.get_text())
            elif font_size == 11:
                print("\t", "Heading 2:", para.get_text())

Ok got it, thanks for your help!

1 Like

@denis.shvydkiy I want to identify the table which has empty cells and add comment there. How can I do that in python using aspose.words?
Ex: In the last two tables of this document has 1-2 empty cells in last column so I want to add comment there.
Document.docx (7.0 KB)

@harshitha112000 You can use the following code to achieve this:

doc = aw.Document("C:\\Temp\\in.docx")

for c in doc.get_child_nodes(aw.NodeType.CELL, True):
    cell = c.as_cell()
    #check whether cell has any visible content.
    cell_text = cell.to_string(aw.SaveFormat.TEXT).strip()
    if cell_text == "" and cell.get_child_nodes(aw.NodeType.SHAPE, True).count == 0:
        # add comment.
        comment = aw.Comment(doc, "John Doe", "JD", datetime.date.today())
        comment_paragraph = aw.Paragraph(doc)
        comment_paragraph.append_child(aw.Run(doc, "Empty Cell"))
        comment.append_child(comment_paragraph)
        cell.first_paragraph.append_child(comment)

doc.save("C:\\Temp\\out.docx")

Please see our documentation to learn how to work with comments:
https://docs.aspose.com/words/python-net/working-with-comments/

It’s working as expected. Thanks

1 Like

@alexey.noskov, @denis.shvydkiy How to perform multiple tasks and save it in single document? Ex: I want to add comment to the empty cell of table in a document and I want to search, highlight and add comment to certain words of same document. I am able to do both this separately but when I try to do it together and save the document it’s not reflecting in the document. Can you please help me to perform both the tasks on same document and save it?

@harshitha112000 Please make sure you perform the operation on the same instance of the Document. If problem still persist, please share simple code and documents that will allow us to reproduce the problem. We will check the issue and provide you more information.

This is the python-docx code, can we perform same function using aspose.words?

Document.docx (7.2 KB)

doc = docx.Document(f_path)
lists = ['Supported business process(es)','Project Life Cycle','User Requirement Specification (URS)','Functional Specification (FS)','Training','books']

for list1 in lists:
    for paragraph in doc.paragraphs:
        if paragraph.text.startswith(list1):
            for run in paragraph.runs:
                if run.text in list1:

                    len1 = len(run.text)
                    l1 = list1[0:len1]

                    x = run.text.split(l1)
                    run.clear()
                    for i in range(len(x)-1):
                        run.add_text(x[i])
                        run.add_text(list1)
                        run.font.highlight_color = WD_COLOR_INDEX.BLUE

doc.save("docx-higlight.docx")

@harshitha112000 You can easily achieve this using find/replace functionality:

import aspose.words as aw
import aspose.pydrawing as pydraw

doc = aw.Document("C:\\Temp\\in.docx")

lists = ['Supported business process(es)','Project Life Cycle','User Requirement Specification (URS)','Functional Specification (FS)','Training','books']

options = aw.replacing.FindReplaceOptions()
options.apply_font.highlight_color = pydraw.Color.blue
options.use_substitutions = True

for list1 in lists:
    doc.range.replace(list1, "$0", options)

doc.save("C:\\Temp\\out.docx")

But I want to highlight only if those words are bold.

@harshitha112000 You can use code like this:

import aspose.words as aw
import aspose.pydrawing as pydraw

doc = aw.Document("C:\\Temp\\in.docx")
doc.join_runs_with_same_formatting()

lists = ['Supported business process(es)','Project Life Cycle','User Requirement Specification (URS)','Functional Specification (FS)','Training','books']

options = aw.replacing.FindReplaceOptions()
options.apply_font.highlight_color = pydraw.Color.blue
options.use_substitutions = True

for r in doc.get_child_nodes(aw.NodeType.RUN, True):
    run = r.as_run()
    if run.font.bold:
        for list1 in lists:
            run.range.replace(list1, "$0", options)

doc.save("C:\\Temp\\out.docx")