Calc Direct Cell Numbers
The style_numbers_numbers()
method gives you the similar options
as Calc’s Font Dialog, but without the dialog. as seen in Fig. 352.
There are several other style_numbers_*
methods that can be used to set the number format of a cell or range
such as, style_numbers_currency()
and style_numbers_percent()
.
Apply the Numbers to a cell
Setup
from __future__ import annotations
import uno
from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
def main() -> int:
with Lo.Loader(connector=Lo.ConnectSocket()):
doc = CalcDoc.create_doc(visible=True)
sheet = doc.sheets[0]
Lo.delay(500)
doc.zoom_value(400)
cell = sheet["A1"]
cell.value = -123.0
cell.style_numbers_numbers(
num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2_RED,
)
Lo.delay(1_000)
doc.close()
return 0
if __name__ == "__main__":
SystemExit(main())
Setting the Numbers format
The NumberFormatIndexEnum
contains the values in API NumberFormatIndex for easy lookup.
from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
# ... other code
cell = sheet["A1"]
cell.value = -123.0
cell.style_numbers_numbers(
num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2_RED,
)
Running the above code will produce the following output in Fig. 353 and Fig. 354.
Getting the number format from a cell
# ... other code
f_style = cell.style_numbers_numbers_get()
assert f_style is not None
Apply the Number to a range
Setup
from __future__ import annotations
import uno
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
def main() -> int:
with Lo.Loader(connector=Lo.ConnectSocket()):
doc = CalcDoc.create_doc(visible=True)
sheet = doc.sheets[0]
Lo.delay(500)
doc.zoom_value(400)
rng = sheet.rng("A1:B1")
sheet.set_array(values=[[0.000000034, 0.000000013]], range_obj=rng)
cell_rng = sheet.get_range(range_obj=rng)
cell_rng.style_numbers_scientific()
Lo.delay(1_000)
doc.close()
return 0
if __name__ == "__main__":
SystemExit(main())
Setting the Numbers format
# ... other code
cell_rng = sheet.get_range(range_obj=rng)
cell_rng.style_numbers_scientific()
Running the above code will produce the following output in Fig. 355 and Fig. 356.
Getting the number format from a range
# ... other code
f_style = cell_rng.style_numbers_numbers_get()
assert f_style is not None