Calc Style Cell / Range
Overview
Applying Cell Styles can be accomplished using the style_by_name()
method.
The StyleCellKind
enum is used to lookup the style to be applied.
Setup
General function used to run these examples.
from __future__ import annotations
import uno
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
from ooodev.format.calc.style import StyleCellKind
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 = "Hello"
cell.style_by_name(StyleCellKind.ACCENT_1)
name = cell.style_by_name_get()
assert name == str(StyleCellKind.ACCENT_1)
Lo.delay(1_000)
doc.close()
return 0
if __name__ == "__main__":
SystemExit(main())
Working with Cell Styles
Apply Style to a Cell
A cell style can be applied while setting the value of a cell.
from ooodev.format.calc.style import StyleCellKind
# ... other code
cell = sheet["A1"]
cell.value = "Hello"
cell.style_by_name(StyleCellKind.ACCENT_1)
Result be seen in Fig. 461.
Get Style from a Cell
# ... other code
name = cell.style_by_name_get()
assert name == str(StyleCellKind.ACCENT_1)
Set Default Style for a Cell
By calling style_by_name()
without any arguments, the default style will be applied.
# ... other code
cell.style_by_name()
Working with Cell Range Styles
Apply Style to a Range
In this example we will set the values of a range and apply a style.
# ... other code
rng = sheet.rng("A1:B1")
sheet.set_array(values=[[101, 22]], range_obj=rng)
cell_rng = sheet.get_range(range_obj=rng)
cell_rng.style_by_name(StyleCellKind.ACCENT_1)
Result be seen in Fig. 462.
Get Style from a Range
# ... other code
name = cell_rng.style_by_name_get()
assert name == str(StyleCellKind.ACCENT_1)
Set Default Style for a Cell Range
By calling style_by_name()
without any arguments, the default style will be applied.
# ... other code
cell_rng.style_by_name()