Calc Direct Cell Numbers (Static)

The ooodev.format.calc.direct.cell.numbers.Numbers class gives you the similar options as Calc’s Font Dialog, but without the dialog. as seen in Fig. 407.

Calc Format Cell dialog Numbers

Fig. 407 Calc Format Cell dialog Numbers

Apply the Numbers 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.numbers import Numbers
from ooodev.format.calc.direct.cell.numbers import NumberFormatEnum, NumberFormatIndexEnum

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")
        num_style = Numbers(num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2_RED)
        Calc.set_val(value=-123.0, cell=cell, styles=[num_style])

        f_style = Numbers.from_obj(cell)
        assert f_style is not None

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


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

Setting the Numbers format

The NumberFormatIndexEnum contains the values in API NumberFormatIndex for easy lookup.

num_style = Numbers(num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2_RED)
Calc.set_val(value=-123.0, cell=cell, styles=[num_style])

Running the above code will produce the following output in Fig. 408 and Fig. 409.

Calc Cell

Fig. 408 Calc Cell

Calc Format Cell dialog Number Format set

Fig. 409 Calc Format Cell dialog Number Format set

Getting the number format from a cell

# ... other code

f_style = Numbers.from_obj(cell)
assert f_style is not None

Apply the Number 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.numbers import Numbers

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)

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

        num_style = Numbers().scientific
        num_style.apply(rng)

        f_style = Numbers.from_obj(rng)
        assert f_style is not None

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


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

Setting the Numbers format

# ... other code
num_style = Numbers().scientific
num_style.apply(rng)

Running the above code will produce the following output in Fig. 410 and Fig. 411.

Calc Cell

Fig. 410 Calc Cell

Calc Format Cell dialog Number Format set

Fig. 411 Calc Format Cell dialog Number Format set

Getting the number format from a range

# ... other code

f_style = Numbers.from_obj(rng)
assert f_style is not None