Unable to go out of a text box to write a new paragraph: python

I am new to Aspose python library. I am trying to create something like


My idea is to create a paragraph, insert a text box in it then create another paragraph that will show the text “Title of this chapter”.
Here is my code:

import aspose.words as aw
import aspose.pydrawing as drawing

hex_color = "#98be25"
rgb_color = drawing.ColorTranslator.from_html(hex_color)

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.insert_paragraph()
# Insert another textbox with specific margins.
text_box_shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 100, 120)
text_box = text_box_shape.text_box
text_box.internal_margin_top = 15
text_box.internal_margin_bottom = 15
text_box.internal_margin_left = 15
text_box.internal_margin_right = 15

# Set the outline color of the text box to be transparent (invisible)
text_box_shape.stroke_color = drawing.Color.transparent

# Move the builder cursor to the end of the existing paragraph
builder.move_to(text_box_shape.last_paragraph)

# Set font name, size, and alignment
builder.font.name = "Lucida Bright"
builder.font.size = 9
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER

builder.writeln()

# Write text
builder.write("Capitolo")

# Insert a line break
builder.writeln()

# Set font name, size, and alignment
builder.font.name = "Lucida Bright"
builder.font.size = 43
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER

# Write text with different font and size
builder.write("10")

# Set the fill color for the textbox
text_box_shape.fill_color = rgb_color


builder.insert_paragraph()

builder.writeln()
builder.writeln()

builder.paragraph_format.style_name = "Heading 1"
builder.write("Title of this chapter")

# Save the document
doc.save("learn1.docx")

The issue I am facing is that the created paragraph that should hold the text “Title of this chapter” is created inside the text box hence invisible.
Any help is appreciated.

@ansar2024 You just need to move outside of your shape before create heading:

# Set the fill color for the textbox
text_box_shape.fill_color = rgb_color

builder.move_to(text_box_shape.parent_paragraph)

builder.writeln()
builder.writeln()

builder.paragraph_format.style_name = "Heading 1"
builder.write("Title of this chapter")

# Save the document
doc.save("learn1.docx")
1 Like

It works fine now. Thanks a lot.