Write Style Page Class

Applying Page Styles can be accomplished using the ooodev.format.writer.style.Page class.

Setup

General function used to run these examples.

import uno
from ooodev.format.writer.style import Page, WriterStylePageKind
from ooodev.format.writer.modify.page.area import Color as PageAreaColor
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.office.write import Write
from ooodev.utils.color import StandardColor


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.ConnectPipe()):
        doc = Write.create_doc()
        GUI.set_visible(doc=doc)
        Lo.delay(300)
        GUI.zoom(GUI.ZoomEnum.ENTIRE_PAGE)

        pg_cursor = Write.get_page_cursor(doc)
        style = Page(name=WriterStylePageKind.FIRST_PAGE)
        style.apply(pg_cursor)

        color_style = PageAreaColor(
            color=StandardColor.GREEN_DARK2, style_name=WriterStylePageKind.FIRST_PAGE
        )
        color_style.apply(doc)

        cursor = Write.get_cursor(doc)
        Write.append_para(cursor=cursor, text=p_txt)

        Lo.delay(1_000)

        Lo.close_doc(doc)

    return 0



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

Apply Style

In this example we will apply the style First Page to the page cursor. Then we will apply the area color Green Dark 2 to the First page Style. See the Write Modify Page Area for more information on modifying page area styles.

# ... other code
# get the page cursor
pg_cursor = Write.get_page_cursor(doc)
style = Page(name=WriterStylePageKind.FIRST_PAGE)
# apply the style to the page cursor, changing the page style to "First Page"
style.apply(pg_cursor)

# create a page area color style to modify the ``First Page`` style with the color ``Green Dark 2``
color_style = PageAreaColor(
    color=StandardColor.GREEN_DARK2, style_name=WriterStylePageKind.FIRST_PAGE
)
color_style.apply(doc)

# write the paragraph
cursor = Write.get_cursor(doc)
Write.append_para(cursor=cursor, text=p_txt)
Styles applied to First Page

Fig. 1177 Styles applied to First Page

Get Style from Cursor

# ... other code
f_style = Page.from_obj(pg_cursor)
assert f_style.prop_name == style.prop_name