Write Direct Character FontOnly Class
The ooodev.format.writer.direct.char.font.FontOnly
class gives you the same options
as Writer’s Font Dialog, but without the dialog. as seen in Fig. 920.
Setting the font name and size
from ooodev.format.writer.direct.char.font import FontOnly
from ooodev.office.write import Write
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
def main() -> int:
with Lo.Loader(Lo.ConnectPipe()):
doc = Write.create_doc()
GUI.set_visible(doc=doc)
Lo.delay(300)
GUI.zoom(GUI.ZoomEnum.PAGE_WIDTH)
cursor = Write.get_cursor(doc)
font_style = FontOnly(name="Liberation Serif", size=20)
Write.append_para(
cursor=cursor, text="Hello World!", styles=[font_style]
)
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
SystemExit(main())
Running the above code will produce the following output in Fig. 921.
It can bee seen in Fig. 922 that the font size is 20pt.
Getting the font name and size from the document
Continuing from the code example above, we can get the font name and size from the document.
A paragraph cursor object is used to select the first paragraph in the document. The paragraph cursor is then used to get the font style.
# ... other code
para_cursor = Write.get_paragraph_cursor(cursor)
para_cursor.gotoPreviousParagraph(False)
para_cursor.gotoEndOfParagraph(True)
font_style = FontOnly.from_obj(para_cursor)
assert font_style.prop_name == "Liberation Serif"
assert font_style.prop_size.value == 20
para_cursor.gotoEnd(False)