Calc Direct Cell Background

Color

The style_area_color() method is called to set the background color of a cell.

Apply the background color to a cell

Setup

from __future__ import annotations
import uno
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
from ooodev.utils.color import StandardColor

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_area_color(StandardColor.BLUE_LIGHT2)

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

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

Setting the color

# ... other code
cell = sheet["A1"]
cell.value = "Hello"
cell.style_area_color(StandardColor.BLUE_LIGHT2)

Running the above code will produce the following output in Fig. 315 and Fig. 316.

Calc Cell Background Color set

Fig. 315 Calc Cell Background Color set

Calc Format Cell dialog Background Color set

Fig. 316 Calc Format Cell dialog Background Color set

Getting the color from a cell

# ... other code
f_style = cell.style_area_color_get()
assert f_style.prop_color == StandardColor.BLUE_LIGHT2

Apply the background color to a range

Setup

from __future__ import annotations
import uno
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
from ooodev.utils.color import StandardColor


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=[["Hello", "World"]], range_obj=rng
        )
        cell_rng = sheet.get_range(range_obj=rng)
        cell_rng.style_area_color(StandardColor.BLUE_LIGHT2)

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

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

Setting the color

# ... other code
cell_rng.style_area_color(StandardColor.BLUE_LIGHT2)

Running the above code will produce the following output in Fig. 371 and Fig. 317.

Calc Cell Background Color set

Fig. 317 Calc Cell Background Color set

Getting the color from a range

# ... other code

f_style = cell_rng.style_area_color_get()
assert f_style.prop_color == StandardColor.BLUE_LIGHT2