Draw Direct Shape Text Paragraph Alignment

The ooodev.format.draw.direct.para.alignment.Alignment class is used to modify the values seen in Fig. 859 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 import TextColumns
from ooodev.format.draw.direct.text.text import TextAnchor, ShapeBasePointKind
from ooodev.format.draw.direct.para.alignment import Alignment, ParagraphAdjust
from ooodev.format import Styler


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 = 100
        height = 50
        x = 10
        y = 10

        rect = slide.draw_rectangle(x=x, y=y, width=width, height=height)
        sb = []
        for _ in range(12):
            sb.append("Hello World!")
        rect.set_string("\n".join(sb))

        anchor = TextAnchor(anchor_point=ShapeBasePointKind.CENTER, full_width=True)
        align = Alignment(align=ParagraphAdjust.CENTER)
        txt_cols = TextColumns(col_count=2, spacing=0.5)
        Styler.apply(rect.component, anchor, align, txt_cols)

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


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

Fig. 859 Shape Text Columns Dialog

Set shapes text alignment

Setting Text Columns of the shape text is done by using the Alignment class.

The Styler class is used to apply several styles to the shape at one time.

rect = slide.draw_rectangle(x=x, y=y, width=width, height=height)
# ... other code
anchor = TextAnchor(anchor_point=ShapeBasePointKind.CENTER, full_width=True)
align = Alignment(align=ParagraphAdjust.CENTER)
txt_cols = TextColumns(col_count=2, spacing=0.5)
Styler.apply(rect.component, anchor, align, txt_cols)

The dialog results of the setting the shape text columns can be seen in Fig. 859.

Get Shape Text Alignment

We can get the text alignment of the shape by using the Alignment.from_obj() method.

# get the properties from the shape
f_style = Alignment.from_obj(rect.component)
assert f_style.prop_align == ParagraphAdjust.CENTER