Calc Direct Cell Font Only (Static)

The ooodev.format.calc.direct.cell.font.FontOnly class gives you the same options as Calc’s Font Dialog, but without the dialog. as seen in Fig. 405.

Calc Format Cell dialog Font Effects

Fig. 405 Calc Format Cell dialog Font Effects

Apply the font to a cell

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.font import FontOnly

def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = Calc.create_doc()
        sheet = Calc.get_sheet()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        Calc.zoom_value(doc, 400)

        cell = Calc.get_cell(sheet=sheet, cell_name="A1")
        font_style = FontOnly(name="Lucida Calligraphy", size=20, font_style="italic")
        Calc.set_val(value="Hello", cell=cell, styles=[font_style])

        f_style = FontOnly.from_obj(cell)
        assert f_style.prop_name == "Lucida Calligraphy"

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


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

Setting the font

font_style = FontOnly(name="Lucida Calligraphy", size=20, font_style="italic")
Calc.set_val(value="Hello", cell=cell, styles=[font_style])

Running the above code will produce the following output in Fig. 406.

Calc Format Cell dialog Font set

Fig. 406 Calc Format Cell dialog Font set

Getting the font from a cell

# ... other code

f_style = FontOnly.from_obj(cell)
assert f_style.prop_name == "Lucida Calligraphy"

Apply the font to a range

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.font import FontOnly

def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = Calc.create_doc()
        sheet = Calc.get_sheet()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        Calc.zoom(doc, GUI.ZoomEnum.ZOOM_100_PERCENT)

        Calc.set_val(value="Hello", sheet=sheet, cell_name="A1")
        Calc.set_val(value="World", sheet=sheet, cell_name="B1")
        rng = Calc.get_cell_range(sheet=sheet, range_name="A1:B1")

        font_style = FontOnly(name="Lucida Calligraphy", size=20, font_style="italic")
        font_style.apply(rng)

        f_style = FontOnly.from_obj(rng)
        assert f_style.prop_name == "Lucida Calligraphy"

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


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

Setting the font

# ... other code
font_style = FontOnly(name="Lucida Calligraphy", size=20, font_style="italic")
font_style.apply(rng)

Running the above code will produce the following output in Fig. 406.

Getting the font from a range

# ... other code

f_style = FontOnly.from_obj(rng)
assert f_style.prop_name == "Lucida Calligraphy"