It’s working fine. Thanks.
I have one issue, while saving the document it’s not saving the entire document but only the first few pages. Why is that happening?
No. Maybe thats the reason. Thanks
I had one more doubt. How to identify heading 1 and heading 2 and highlight, add comments using this library for the Document.docx which I shared earlier?
@harshitha112000, I don’t see heading 1 and heading 2 in Document.docx. If you meant “Objective” and “How to read this document” you find them by adding the &p metacharacter to the end of pattern:
from datetime import datetime
from aspose.pydrawing import Color
from aspose.words import Comment, Document, DocumentBuilder, NodeType, Paragraph, Run
from aspose.words.replacing import FindReplaceOptions
doc = Document("Document.docx")
patterns = ["Objective&p", "How to read this document&p" ]
options = FindReplaceOptions()
options.apply_font.highlight_color = Color.red
for pattern in patterns:
doc.range.replace(pattern, pattern, options)
builder = DocumentBuilder(doc)
for node in doc.get_child_nodes(NodeType.RUN, True):
run = node.as_run()
if run.font.highlight_color == Color.red:
comment = Comment(doc, "Harshitha", "H", datetime.today())
comment.paragraphs.add(Paragraph(doc))
comment.first_paragraph.runs.add(Run(doc, "Comment text"))
builder.move_to(run)
builder.current_paragraph.insert_before(comment, run)
doc.save("out_python_comments.docx")
In Document.docx, Description is Heading1 and Project description is Heading 2. I asked for this. I used istitle() function but it is identifying only heading 1
@harshitha112000, in my understanding Heading 1 and Heading 2 are styles with such names. In Document.docx attached in this thread, “Description” and “Project description” have the Normal style applied, so they are not considered as headings.
There is the is_heading property that returns true for heading paragraphs, for instance:
from aspose.words import Document, NodeType
doc = Document("Document.docx")
for node in doc.get_child_nodes(NodeType.PARAGRAPH, True):
para = node.as_paragraph()
if para.paragraph_format.is_heading:
print(para.get_text())
In case of Document.docx this code prints nothing. This means no paragraphs with heading style are present in the document.
Could you please share the code example how you identify Description as Heading 1 and Project description as Heading2 using the istitle() function?
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!
@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