TextBox class

TextBox class

Defines attributes that specify how a text is displayed inside a shape. To learn more, visit the Working with Shapes documentation article.

Remarks

Use the Shape.text_box property to access text properties of a shape. You do not create instances of the TextBox class directly.

Properties

NameDescription
fit_shape_to_textDetermines whether Microsoft Word will grow the shape to fit text.
internal_margin_bottomSpecifies the inner bottom margin in points for a shape.
internal_margin_leftSpecifies the inner left margin in points for a shape.
internal_margin_rightSpecifies the inner right margin in points for a shape.
internal_margin_topSpecifies the inner top margin in points for a shape.
layout_flowDetermines the flow of the text layout in a shape.
nextReturns or sets a TextBox that represents the next TextBox in a sequence of shapes.
no_text_rotationGets or sets a boolean value indicating either text of the TextBox should not rotate when the shape is rotated.
parentGets a parent shape for the TextBox.
previousReturns a TextBox that represents the previous TextBox in a sequence of shapes.
text_box_wrap_modeDetermines how text wraps inside a shape.
vertical_anchorSpecifies the vertical alignment of the text within a shape.

Methods

NameDescription
break_forward_link()Breaks the link to the next TextBox.
is_valid_link_target(target)Determines whether this TextBox can be linked to the target TextBox.

Examples

Shows how to set the orientation of text inside a text box.

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

text_box_shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 150, 100)
text_box = text_box_shape.text_box

# Move the document builder to inside the TextBox and add text.
builder.move_to(text_box_shape.last_paragraph)
builder.writeln("Hello world!")
builder.write("Hello again!")

# Set the "layout_flow" property to set an orientation for the text contents of this text box.
text_box.layout_flow = layout_flow

doc.save(ARTIFACTS_DIR + "Shape.text_box_layout_flow.docx")

Shows how to get a text box to resize itself to fit its contents tightly.

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

text_box_shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 150, 100)
text_box = text_box_shape.text_box

# Apply these values to both these members to get the parent shape to fit
# tightly around the text contents, ignoring the dimensions we have set.
text_box.fit_shape_to_text = True
text_box.text_box_wrap_mode = aw.drawing.TextBoxWrapMode.NONE

builder.move_to(text_box_shape.last_paragraph)
builder.write("Text fit tightly inside textbox.")

doc.save(ARTIFACTS_DIR + "Shape.text_box_fit_shape_to_text.docx")

Shows how to set internal margins for a text box.

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

# Insert another textbox with specific margins.
text_box_shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 100, 100)
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

builder.move_to(text_box_shape.last_paragraph)
builder.write("Text placed according to textbox margins.")

doc.save(ARTIFACTS_DIR + "Shape.text_box_margins.docx")

See Also