Draw Direct Shape Text - Anchor

The ooodev.format.draw.direct.text.text.TextAnchor class is used to modify the values seen in Fig. 871 of a shape.

Setup

from __future__ import annotations
import uno

from ooodev.draw import Draw, DrawDoc, ZoomKind
from ooodev.loader.lo import Lo
from ooodev.format.draw.direct.text.text import Spacing as TextSpacing
from ooodev.format.draw.direct.text.text import TextAnchor, ShapeBasePointKind


def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = DrawDoc(Draw.create_draw_doc())
        doc.set_visible()
        Lo.delay(500)
        doc.zoom(ZoomKind.ZOOM_75_PERCENT)

        slide = doc.get_slide()

        width = 50
        height = 50
        x = 10
        y = 10

        rect = slide.draw_rectangle(x=x, y=y, width=width, height=height)
        cursor = rect.get_shape_text_cursor()
        cursor.append_para("Hello World!")

        txt_anchor = TextAnchor(
            anchor_point=ShapeBasePointKind.TOP_CENTER, full_width=True
        )
        txt_anchor.apply(rect.component)

        f_style = TextAnchor.from_obj(rect.component)
        assert f_style.prop_full_width is True
        assert f_style.prop_anchor_point == ShapeBasePointKind.TOP_CENTER

        Lo.delay(1_000)
        doc.close_doc()
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
Shape Text Anchor Point Dialog

Fig. 871 Shape Text Anchor Point Dialog

Set Text Anchor of Shape Text

Setting Text Anchor Point of the shape text is done by using the TextAnchor class.

from ooodev.format.draw.direct.text.text import TextAnchor, ShapeBasePointKind
# ... other code

rect = slide.draw_rectangle(x=x, y=y, width=width, height=height)
# ... other code
txt_anchor = TextAnchor(
    anchor_point=ShapeBasePointKind.TOP_CENTER, full_width=True
)
txt_anchor.apply(rect.component)

The results of the setting the shape text anchor can be seen in Fig. 872.

Shape with Text Anchor Set

Fig. 872 Shape with Text Anchor Set

Get Shape Text Anchor

We can get the text anchor of the shape by using the TextAnchor.from_obj() method.

from ooodev.format.draw.direct.text.text import TextAnchor
# ... other code

# get the properties from the shape
f_style = TextAnchor.from_obj(rect.component)
assert f_style.prop_full_width is True
assert f_style.prop_anchor_point == ShapeBasePointKind.TOP_CENTER