Write Direct Paragraph Alignment

Overview

Writer has an Alignment dialog tab.

The ooodev.format.writer.direct.para.alignment.Alignment class is used to set the paragraph alignment.

Writer Paragraph Alignment dialog

Fig. 933 Writer Paragraph Alignment dialog.

Setup

from ooodev.office.write import Write
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.writer.direct.para.alignment import Alignment, LastLineKind, ParagraphAdjust

def main() -> int:
    p_txt = (
        "To Sherlock Holmes she is always THE woman. I have seldom heard"
        " him mention her under any other name. In his eyes she eclipses"
        " and predominates the whole of her sex. It was not that he felt"
        " any emotion akin to love for Irene Adler. All emotions, and that"
        " one particularly, were abhorrent to his cold, precise but"
        " admirably balanced mind. He was, I take it, the most perfect"
        " reasoning and observing machine that the world has seen, but as a"
        " lover he would have placed himself in a false position. He never"
        " spoke of the softer passions, save with a gibe and a sneer. They"
        " were admirable things for the observer--excellent for drawing the"
        " veil from men's motives and actions. But for the trained reasoner"
        " to admit such intrusions into his own delicate and finely"
        " adjusted temperament was to introduce a distracting factor which"
        " might throw a doubt upon all his mental results. Grit in a"
        " sensitive instrument, or a crack in one of his own high-power"
        " lenses, would not be more disturbing than a strong emotion in a"
        " nature such as his. And yet there was but one woman to him, and"
        " that woman was the late Irene Adler, of dubious and questionable memory."
    )

    with Lo.Loader(Lo.ConnectSocket()):
        doc = Write.create_doc()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        GUI.zoom(GUI.ZoomEnum.ENTIRE_PAGE)

        cursor = Write.get_cursor(doc)
        al = Alignment().align_left
        Write.append_para(cursor=cursor, text=p_txt, styles=[al])
        Lo.delay(1_000)
        Lo.close_doc(doc)
    return 0


if __name__ == "__main__":
    SystemExit(main())

Examples

Generally, if paragraph alignment is set using Write then the alignment style only applies to current paragraph.

Align Default

# ... other code
al_default = Alignment().default
Write.append_para(cursor=cursor, text=p_txt, styles=[al_default])
Writer Paragraph Alignment Default.

Fig. 934 Writer Paragraph Alignment Default.

Align Left

# ... other code
al = Alignment().align_left
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Left.

Fig. 935 Writer Paragraph Alignment Left.

Align Center

# ... other code
al = Alignment().align_center
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Center

Fig. 936 Writer Paragraph Alignment Center.

Align Right

# ... other code
al = Alignment().align_right
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Right

Fig. 937 Writer Paragraph Alignment Right.

Align Justified

# ... other code
al = Alignment().justified
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Justified

Fig. 938 Writer Paragraph Alignment Justified.

Justified Last Line

It is possible to also set the last line when using Align Justified.

Justify last line center.

# ... other code
al = Alignment(align_last=LastLineKind.CENTER).justified
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Justified, Center

Fig. 939 Writer Paragraph Alignment Justified, Center.

Justify last line justified.

# ... other code
al = Alignment(align_last=LastLineKind.JUSTIFY).justified
Write.append_para(cursor=cursor, text=p_txt, styles=[al])
Writer Paragraph Alignment Justified, JUSTIFY

Fig. 940 Writer Paragraph Alignment Justified, JUSTIFY.

Set Cursor

Sometimes it may be necessary to set the cursor style for many paragraphs. The best way to accomplish this is to set the cursor style before appending paragraphs.

# ... other code
cursor = Write.get_cursor(doc)
al = Alignment(align_last=LastLineKind.CENTER).justified
al.apply(cursor)
Write.append_para(cursor=cursor, text=p_txt)
Write.append_para(cursor=cursor, text=p_txt)
Writer Paragraph Alignment via cursor

Fig. 941 Writer Paragraph Alignment via cursor.

For resetting cursor to default, see Resetting Paragraph Style to Default.

Getting Alignment from Paragraph

A paragraph cursor object is used to select the first paragraph in the document. The paragraph cursor is then used to get the style.

# ... other code

cursor = Write.get_cursor(doc)
al = Alignment().align_right
Write.append_para(cursor=cursor, text=p_txt, styles=[al])

para_cursor = Write.get_paragraph_cursor(cursor)
para_cursor.gotoPreviousParagraph(False)
para_cursor.gotoEndOfParagraph(True)

para_align = Alignment.from_obj(para_cursor)
assert para_align.prop_align == ParagraphAdjust.RIGHT