Calc Direct Cell Borders

Overview

Calc has a dialog, as seen in Fig. 318, that sets cell borders. In this section we will look the various classes that set the same options.

The ooodev.format.calc.direct.cell.borders.Borders class is used to set the border values.

Calc Format Cells Borders dialog

Fig. 318 Calc Format Cells Borders dialog.

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.direct.cell.borders import Side
from ooodev.utils.color import CommonColor


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(130)

        rng = sheet.rng("B2:F6")
        cell_rng = sheet.get_range(range_obj=rng)
        cell_rng.style_borders(border_side=Side(color=CommonColor.BLUE))

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

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

Examples

Single Cell

Default Border

Calling style_borders_default() will create a default border for a cell or a range.

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_default()
Cell with default border

Fig. 319 Cell with default border.

Removing Border

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_default()
# ...
# remove border
cell.style_borders_clear()

Colored border

The style_borders_sides() method can be used when only all four sides are to be styled at once. Here The style_borders_sides() is used to set the border color to red.

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_sides(color=CommonColor.RED)
Cell with colored border

Fig. 320 Cell with colored border.

Applying border to a side

Apply green border to left side.

The style_borders() method can also take left, right, top, bottom, vertical, horizontal, diagonal_down and diagonal_up arguments as sides. In this case just pass in the left side.

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell.style_borders(
    left=Side(color=CommonColor.GREEN),
)
Cell with left colored border

Fig. 321 Cell with left colored border.

Apply border with increased size

Passing width argument to Side() controls border width.

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
side_left_right = Side(color=CommonColor.GREEN, width=1.8)
cell.style_borders(
    left=side_left_right, right=side_left_right
)
Cell with left and right colored border

Fig. 322 Cell with left and right colored border.

Apply different top and side colors

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
side_top_bottom = Side(color=CommonColor.CHARTREUSE, width=1.3)
side_left_right = Side(color=CommonColor.ROYAL_BLUE, width=1.3)
cell.style_borders(
    top=side_top_bottom,
    bottom=side_top_bottom,
    left=side_left_right,
    right=side_left_right,
)
Cell with left and right colored border

Fig. 323 Cell with left and right colored border.

Apply Diagonal border

Passing diagonal_up and diagonal_down arguments to Borders allows for diagonal lines.

UP

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders(
    diagonal_up=Side(color=CommonColor.RED)
)
Cell with diagonal up colored border

Fig. 324 Cell with diagonal up colored border.

DOWN

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders(
    diagonal_down=Side(color=CommonColor.RED)
)
Cell with diagonal down colored border

Fig. 325 Cell with diagonal down colored border.

Applying different style border

Using BorderLineKind enumeration it is possible to change the border style to many different configurations.

In this example the border style is set to Dash-dot.

from ooodev.format.calc.direct.cell.borders import BorderLineKind

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_sides(
    line=BorderLineKind.DASH_DOT,
    color=CommonColor.DARK_GREEN
)
Cell with dash-dot colored border

Fig. 326 Cell with dash-dot colored border.

Apply Shadow to cell

Using the Shadow class shadows with a variety of options can be added to a cell.

In this example the default shadow is used.

from ooodev.format.calc.direct.cell.borders import Shadow

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_sides(
    color=CommonColor.BLUE,
    shadow=Shadow(),
)
Cell with blue colored border and default shadow

Fig. 327 Cell with blue colored border and default shadow.

Apply padding to a cell

Using the Padding class it is possible to add padding to a cell. Padding can take left, right, top, bottom arguments or all can be use to apply even padding to all sides at one.

from ooodev.format.calc.direct.cell.borders import Padding

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders_sides(
    color=CommonColor.BLUE,
    padding=Padding(left=1.5),
)
Cell with blue colored border and left padding

Fig. 328 Cell with blue colored border and left padding.

Calc Format Cells Borders dialog

Fig. 329 Calc Format Cells Borders dialog

Cumulative borders

Applying more then one border style to a cell keeps previous formatting.

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell = sheet["B2"]
cell.value = "Hello World"
cell.style_borders(diagonal_up=Side(color=CommonColor.RED))
cell.style_borders(diagonal_down=Side(color=CommonColor.BLUE))
Cell with cumulative borders

Fig. 330 Cell with cumulative borders.

Range of Cells

Default Borders

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_default()
Range with default borders

Fig. 331 Range with default borders.

Removing Borders

Applying Border().empty to a cell or a range will clear all formatting.

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_default()
# ...
cell_rng.style_borders_clear()

Colored borders

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_sides(color=CommonColor.RED)
Range with colored borders

Fig. 332 Range with colored borders.

Applying border to a side

Apply green border to left side.

Borders constructor can also take left, right, top, bottom, vertical, horizontal, diagonal_down and diagonal_up arguments as sides. In this case just pass in the left side.

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders(left=Side(color=CommonColor.GREEN))
Range with left colored border

Fig. 333 Range with left colored border.

Apply border with increased size

Passing width argument to Side() controls border width.

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
side_left_right = Side(color=CommonColor.GREEN, width=1.8)
cell_rng.style_borders(
    left=side_left_right, right=side_left_right
)
Range with left and right colored border with increased width

Fig. 334 Range with left and right colored border with increased width.

Apply different top and side colors

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
side_top_bottom = Side(color=CommonColor.CHARTREUSE, width=1.3)
side_left_right = Side(color=CommonColor.ROYAL_BLUE, width=1.3)
cell_rng.style_borders(
    top=side_top_bottom,
    bottom=side_top_bottom,
    left=side_left_right,
    right=side_left_right,
)
Range different top and bottom border colors

Fig. 335 Range different top and bottom border colors.

Apply Diagonal border

UP

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders(
    border_side=Side(),
    diagonal_up=Side(color=CommonColor.RED),
)
Range with diagonal up border

Fig. 336 Range with diagonal up border.

DOWN

from ooodev.format.calc.direct.cell.borders import Side

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders(
    border_side=Side(),
    diagonal_down=Side(color=CommonColor.RED),
)
Range with diagonal up border

Fig. 337 Range with diagonal up border.

Applying different style border

Using BorderLineKind enumeration it is possible to change the border style to many different configurations.

In this example the border style is set to Dash-dot.

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_sides(
    line=BorderLineKind.DASH_DOT,
    color=CommonColor.DARK_GREEN,
)
Range with dash-dot border

Fig. 338 Range with dash-dot border.

Apply Shadow to range

Using the Shadow class shadows with a variety of options can be added to a range.

In this example the default shadow is used.

from ooodev.format.calc.direct.cell.borders import Shadow

# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_sides(
    color=CommonColor.BLUE,
    shadow=Shadow(),
)
Range with border and shadow

Fig. 339 Range with border and shadow.

Apply horizontal and vertical lines

This example sets outer border to blue for all sides. Horizontal lines are set to dash-dash-dot with color of green. Vertical lines are set to double with a color of red.

from ooodev.format.calc.direct.cell.borders import Side
from ooodev.format.calc.direct.cell.borders import BorderLineKind

# ... other code
ell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders(
    border_side=Side(color=CommonColor.BLUE),
    horizontal=Side(line=BorderLineKind.DASH_DOT_DOT, color=CommonColor.GREEN),
    vertical=Side(line=BorderLineKind.DOUBLE, color=CommonColor.RED),
)
Range various border styles

Fig. 340 Range various border styles.

Multiple Styles

from ooodev.format.calc.direct.cell.borders import Side
from ooodev.format.calc.direct.cell.borders import BorderLineKind

# ... other code
with doc:
    # lock document controllers for fast processing and avoid flickering.
    cell_rng = sheet.get_range(range_name="B2:F6")
    cell_rng.style_borders(
        border_side=Side(color=CommonColor.BLUE_VIOLET, width=1.3),
        diagonal_up=Side(color=CommonColor.RED),
        diagonal_down=Side(color=CommonColor.RED),
    )

    cell_rng = sheet.get_range(range_name="C3:E5")
    cell_rng.style_borders_clear()
    cell_rng.style_borders(
        border_side=Side(color=CommonColor.BLUE),
        horizontal=Side(line=BorderLineKind.DASH_DOT_DOT, color=CommonColor.GREEN),
        vertical=Side(line=BorderLineKind.DOUBLE, color=CommonColor.RED),
    )
Range multiple border styles

Fig. 341 Range multiple border styles.