Write Direct Table Borders
The ooodev.format.writer.direct.table.borders.Borders
class can be used to set the borders of a table.
There are also other ways to set the borders of a table as noted in the examples below.
Setup
General function used to run these examples.
from ooodev.format.writer.direct.table.borders import (
Borders,
Shadow,
Side,
BorderLineKind,
Padding,
LineSize,
)
from ooodev.write import WriteDoc
from ooodev.utils.color import CommonColor
from ooodev.utils.color import StandardColor
from ooodev.loader import Lo
from ooodev.utils.table_helper import TableHelper
def main() -> int:
with Lo.Loader(Lo.ConnectPipe()):
doc = WriteDoc.create_doc(visible=True)
Lo.delay(300)
doc.zoom(ZoomKind.ZOOM_100_PERCENT)
cursor = doc.get_cursor()
tbl_data = TableHelper.make_2d_array(num_rows=5, num_cols=5)
# bg_img_style = TblBgImg.from_preset(PresetImageKind.PAPER_TEXTURE)
table = cursor.add_table(
table_data=tbl_data,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
first_row_header=False,
)
blue_side = Side(line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK)
green_side = Side(line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN)
bdr_style = Borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
shadow=Shadow(color=StandardColor.BLUE_DARK2),
)
bdr_style.apply(table.component)
# getting the table properties
tbl_bdr_style = Borders.from_obj(table.component)
assert tbl_bdr_style is not None
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
sys.exit(main())
Examples
Borders
Borders Simple
Set using style_direct
Table color can also be set using style_direct
property of the table.
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
table.style_direct.style_borders_side()
Set using styles
# ... other code
bdr_style = Borders(border_side=Side())
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
styles=[bdr_style],
)
Set using table_border2
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
table.table_border2.left_line.color = StandardColor.RED_DARK1
table.table_border2.left_line.line_width = UnitPT(float(LineSize.MEDIUM))
table.table_border2.left_line.line_style = BorderLineKind.SOLID
table.table_border2.right_line = table.table_border2.left_line
Set using style_direct
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
default_side = Side()
red_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.RED_DARK1, width=LineSize.MEDIUM
)
table.style_direct.style_borders(
left=red_side, right=red_side, top=default_side, bottom=default_side
)
Set using styles
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
default_side = Side()
red_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.RED_DARK1, width=LineSize.MEDIUM
)
bdr_style = Borders(left=red_side, top=default_side, bottom=default_side, right=red_side)
bdr_style.apply(table.component)
Borders Set Horizontal & Vertical
Set using table_border2
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
table.table_border2.left_line.color = StandardColor.BLUE_DARK2
table.table_border2.left_line.line_width = UnitPT(float(LineSize.THICK))
table.table_border2.left_line.line_style = BorderLineKind.SOLID
table.table_border2.right_line = table.table_border2.left_line
table.table_border2.top_line = table.table_border2.left_line
table.table_border2.bottom_line = table.table_border2.left_line
table.table_border2.vertical_line.color = StandardColor.GREEN_DARK1
table.table_border2.vertical_line.line_width = UnitPT(float(LineSize.THIN))
table.table_border2.vertical_line.line_style = BorderLineKind.SOLID
table.table_border2.horizontal_line = table.table_border2.vertical_line
Set using style_direct
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
blue_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK
)
green_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN
)
table.style_direct.style_borders(
border_side=blue_side, vertical=green_side, horizontal=green_side
)
Set using styles
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
blue_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK
)
green_side = Side(
line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN
)
bdr_style = Borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
)
bdr_style.apply(table.component)
Padding
Borders & Padding
Set using table_border2
from ooodev.units import UnitMM
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
table.table_border2.left_line.color = StandardColor.BLUE_DARK2
table.table_border2.left_line.line_width = UnitPT(float(LineSize.THICK))
table.table_border2.left_line.line_style = BorderLineKind.SOLID
table.table_border2.right_line = table.table_border2.left_line
table.table_border2.top_line = table.table_border2.left_line
table.table_border2.bottom_line = table.table_border2.left_line
table.table_border2.vertical_line.color = StandardColor.GREEN_DARK1
table.table_border2.vertical_line.line_width = UnitPT(float(LineSize.THIN))
table.table_border2.vertical_line.line_style = BorderLineKind.SOLID
table.table_border2.horizontal_line = table.table_border2.vertical_line
table.table_border2.distance = UnitMM(5)
Set using style_direct
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
blue_side = Side(line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK)
green_side = Side(line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN)
padding = Padding(all=5)
table.style_direct.style_borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
padding=padding,
)
Set using styles
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
blue_side = Side(line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK)
green_side = Side(line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN)
padding = Padding(all=5)
bdr_style = Borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
padding=padding,
)
bdr_style.apply(table.component)
Shadow
Borders & Shadow
Set using shadow_format
from ooodev.units import UnitMM
from ooo.dyn.table.shadow_location import ShadowLocation
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
table.table_border2.left_line.color = StandardColor.BLUE_DARK2
table.table_border2.left_line.line_width = UnitPT(float(LineSize.THICK))
table.table_border2.left_line.line_style = BorderLineKind.SOLID
table.table_border2.right_line = table.table_border2.left_line
table.table_border2.top_line = table.table_border2.left_line
table.table_border2.bottom_line = table.table_border2.left_line
table.table_border2.vertical_line.color = StandardColor.GREEN_DARK1
table.table_border2.vertical_line.line_width = UnitPT(float(LineSize.THIN))
table.table_border2.vertical_line.line_style = BorderLineKind.SOLID
table.table_border2.horizontal_line = table.table_border2.vertical_line
table.shadow_format.color = StandardColor.BLUE_DARK2
table.shadow_format.location = ShadowLocation.BOTTOM_RIGHT
table.shadow_format.is_transparent = False
table.shadow_format.shadow_width = UnitMM(1.76)
Set using style_direct
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
tbl_bg_color=CommonColor.LIGHT_BLUE,
tbl_fg_color=CommonColor.BLACK,
)
blue_side = Side(line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK)
green_side = Side(line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN)
table.style_direct.style_borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
shadow=Shadow(color=StandardColor.BLUE_DARK2),
)
Set using styles
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
)
blue_side = Side(line=BorderLineKind.SOLID, color=StandardColor.BLUE_DARK2, width=LineSize.THICK)
green_side = Side(line=BorderLineKind.SOLID, color=StandardColor.GREEN_DARK1, width=LineSize.THIN)
bdr_style = Borders(
border_side=blue_side,
vertical=green_side,
horizontal=green_side,
shadow=Shadow(color=StandardColor.BLUE_DARK2),
)
bdr_style.apply(table.component)
Getting the Borders from the table
# ... other code
# getting the table properties
tbl_bdr_style = Borders.from_obj(table.component)
assert tbl_bdr_style is not None