Calc Direct Cell Borders (Static)

Overview

Calc has a dialog, as seen in Fig. 373, 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. 373 Calc Format Cells Borders dialog.

Setup

General function used to run these examples:

from ooodev.format import Styler
from ooodev.format.calc.direct.cell.borders import Borders, Shadow, Side, BorderLineKind, Padding
from ooodev.office.calc import Calc
from ooodev.utils.color import CommonColor
from ooodev.gui.gui import GUI
from ooodev.loader.lo import Lo


def main() -> int:
    with Lo.Loader(Lo.ConnectSocket()):
        doc = Calc.create_doc()
        sheet = Calc.get_sheet()
        GUI.set_visible(True, doc)
        Lo.delay(300)
        Calc.zoom_value(doc, 130)

        rng_obj = Calc.get_range_obj("B2:F6")
        cr = Calc.get_cell_range(sheet, rng_obj)
        borders = Borders(border_side=Side(color=CommonColor.BLUE))
        Styler.apply(cr, borders)
        Lo.delay(1_000)
        Lo.close_doc(doc)
    return 0


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

Examples

Single Cell

Default Border

Applying Border.default will create a default border for a cell or a range.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
Styler.apply(cell, Borders().default)
Cell with default border

Fig. 374 Cell with default border.

Removing Border

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
Styler.apply(cell, Borders().default)
# ...
# remove border
Styler.apply(cell, Borders().empty)

Colored border

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(border_side=Side(color=CommonColor.RED))
Styler.apply(cell, borders)
Cell with colored border

Fig. 375 Cell with colored border.

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.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
Styler.apply(cell, Borders(left=Side(color=CommonColor.GREEN)))
Cell with left colored border

Fig. 376 Cell with left colored border.

Apply border with increased size

Passing width argument to Side() controls border width.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
side_left_right = Side(color=CommonColor.GREEN, width=1.8)
borders = Borders(left=side_left_right, right=side_left_right)
Styler.apply(cell, borders)
Cell with left and right colored border

Fig. 377 Cell with left and right colored border.

Apply different top and side colors

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
side_top_bottom = Side(color=CommonColor.CHARTREUSE, width=1.3)
side_left_right = Side(color=CommonColor.ROYAL_BLUE, width=1.3)
borders = Borders(
    top=side_top_bottom,
    bottom=side_top_bottom,
    left=side_left_right,
    right=side_left_right,
    )
Styler.apply(cell, borders)
Cell with left and right colored border

Fig. 378 Cell with left and right colored border.

Apply Diagonal border

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

UP

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(diagonal_up=Side(color=CommonColor.RED))
Styler.apply(cell, borders)
Cell with diagonal up colored border

Fig. 379 Cell with diagonal up colored border.

DOWN

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(diagonal_down=Side(color=CommonColor.RED))
Styler.apply(cell, borders)
Cell with diagonal down colored border

Fig. 380 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.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(
    border_side=Side(line=BorderLineKind.DASH_DOT, color=CommonColor.DARK_GREEN)
)
Styler.apply(cell, borders)
Cell with dash-dot colored border

Fig. 381 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.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(border_side=Side(color=CommonColor.BLUE), shadow=Shadow())
Styler.apply(cell, borders)
Cell with blue colored border and default shadow

Fig. 382 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.

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)
borders = Borders(border_side=Side(color=CommonColor.BLUE), padding=Padding(left=1.5))
Styler.apply(cell, borders)
Cell with blue colored border and left padding

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

Calc Format Cells Borders dialog

Fig. 384 Calc Format Cells Borders dialog

Cumulative borders

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

# ... other code
cell_obj = Calc.get_cell_obj("B2")
Calc.set_val(value="Hello World", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet, cell_obj)

border = Borders(diagonal_up=Side(color=CommonColor.RED))
Styler.apply(cell, border)

borders = Borders(diagonal_down=Side(color=CommonColor.BLUE))
Styler.apply(cell, borders)
Cell with cumulative borders

Fig. 385 Cell with cumulative borders.

Range of Cells

Default Borders

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
Styler.apply(cr, Borders().default)
Range with default borders

Fig. 386 Range with default borders.

Removing Borders

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

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
Styler.apply(cr, Borders().default)
# ...
Styler.apply(cr, Borders().empty)

Colored borders

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(border_side=Side(color=CommonColor.RED))
Styler.apply(cr, borders)
Range with colored borders

Fig. 387 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.

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(left=Side(color=CommonColor.GREEN))
Styler.apply(cr, borders)
Range with left colored border

Fig. 388 Range with left colored border.

Apply border with increased size

Passing width argument to Side() controls border width.

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
side_left_right = Side(color=CommonColor.GREEN, width=1.8)
borders = Borders(left=side_left_right, right=side_left_right)
Styler.apply(cr, borders)
Range with left and right colored border with increased width

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

Apply different top and side colors

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
side_top_bottom = Side(color=CommonColor.CHARTREUSE, width=1.3)
side_left_right = Side(color=CommonColor.ROYAL_BLUE, width=1.3)
borders = Borders(
    top=side_top_bottom,
    bottom=side_top_bottom,
    left=side_left_right,
    right=side_left_right,
)
Styler.apply(cr, borders)
Range different top and bottom border colors

Fig. 390 Range different top and bottom border colors.

Apply Diagonal border

UP

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(border_side=Side(), diagonal_up=Side(color=CommonColor.RED))
Styler.apply(cr, borders)
Range with diagonal up border

Fig. 391 Range with diagonal up border.

DOWN

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(border_side=Side(), diagonal_down=Side(color=CommonColor.RED))
Styler.apply(cr, borders)
Range with diagonal up border

Fig. 392 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
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(
    border_side=Side(line=BorderLineKind.DASH_DOT, color=CommonColor.DARK_GREEN)
)
Styler.apply(cr, borders)
Range with dash-dot border

Fig. 393 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.

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(border_side=Side(color=CommonColor.BLUE), shadow=Shadow())
Styler.apply(cr, borders)
Range with border and shadow

Fig. 394 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.

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = 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),
)
Styler.apply(cr, borders)
Range various border styles

Fig. 395 Range various border styles.

Multiple Styles

# ... other code
rng_obj = Calc.get_range_obj("B2:F6")
cr = Calc.get_cell_range(sheet, rng_obj)
borders = Borders(
    border_side=Side(color=CommonColor.BLUE_VIOLET, width=1.3),
    diagonal_up=Side(color=CommonColor.RED),
    diagonal_down=Side(color=CommonColor.RED),
)
Styler.apply(cr, borders)

rng_obj = Calc.get_range_obj("C3:E5")
cr = Calc.get_cell_range(sheet, rng_obj)

Styler.apply(cr, borders)

borders = 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),
)
Styler.apply(cr, borders)
Range multiple border styles

Fig. 396 Range multiple border styles.