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.
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()
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)
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),
)
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
)
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,
)
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)
)
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)
)
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
)
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(),
)
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),
)
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))
Range of Cells
Default Borders
# ... other code
cell_rng = sheet.get_range(range_name="B2:F6")
cell_rng.style_borders_default()
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)
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))
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
)
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,
)
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),
)
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),
)
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,
)
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(),
)
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),
)
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),
)