Write Direct Table Background
The ooodev.format.writer.direct.table.background.Color
and ooodev.format.writer.direct.table.background.Img
classes can used to set the background of a table.
There are also other ways to set the background of a table as noted in the examples below.
Setup
General function used to run these examples.
import sys
import uno
from ooodev.write import WriteDoc
from ooodev.format.writer.direct.table.background import Color as TblBgColor
from ooodev.format.writer.direct.table.background import Img as TblBgImg, PresetImageKind
from ooodev.utils.color import StandardColor
from ooodev.loader import Lo
from ooodev.utils.kind.zoom_kind import ZoomKind
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_color_style = TblBgColor(StandardColor.LIME_LIGHT3)
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
styles=[bg_color_style],
)
# getting the color object
tbl_bg_color_style = TblBgColor.from_obj(table.component)
assert tbl_bg_color_style is not None
Lo.delay(1_000)
doc.close()
return 0
if __name__ == "__main__":
sys.exit(main())
Examples
Background Color
Setting background color
Set using back_color
Table color can be set using back_color
property of the table.
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
)
table.back_color = StandardColor.LIME_LIGHT3
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,
)
table.style_direct.style_area_color(StandardColor.LIME_LIGHT3)
Set using styles
# ... other code
bg_color_style = TblBgColor(StandardColor.LIME_LIGHT3)
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
styles=[bg_color_style],
)
Getting Background Color Object
Get using style_direct
Table color can also be set using style_direct
property of the table.
# ... other code
# getting the table properties
tbl_bg_color_style = table.style_direct.style_area_color_get()
assert tbl_bg_color_style is not None
Get using styles
# ... other code
# getting the table properties
tbl_bg_color_style = TblBgColor.from_obj(table.component)
assert tbl_bg_color_style is not None
Background Image
Setting background Image
Background image has many options. The following example shows how to set a background image from a preset image.
The PresetImageKind
has many preset images to choose from.
Set using style_direct
Table background image can be set using style_direct
property of the table.
from ooodev.format.writer.direct.table.background import PresetImageKind
# ... other code
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
)
table.style_direct.style_area_image_from_preset(PresetImageKind.PAPER_TEXTURE)
Set using styles
from ooodev.format.writer.direct.table.background import PresetImageKind
# ... other code
bg_img_style = TblBgImg.from_preset(PresetImageKind.PAPER_TEXTURE)
table = cursor.add_table(
table_data=tbl_data,
first_row_header=False,
styles=[bg_img_style],
)