Class WriteTable

class ooodev.write.table.write_table.WriteTable(owner, component, lo_inst=None)[source]

Bases: Generic[T], WriteTablePropPartial, EventsPartial, LoInstPropsPartial, WriteDocPropPartial, TextTableComp, WriteTablePropertiesPartial, QiPartial, StylePartial, PropertyChangeImplement, VetoableChangeImplement

Represents writer text content.

__getitem__(key)[source]

Gets the cell as WriteTableCell.

Parameters:

key (Any) – Key. can be a Tuple of (col, row) or a string such as “A1” or a CellObj.

Returns:

Table Cell Object.

Return type:

WriteTableCell

Example

>>> table = doc.tables[0]
>>> cell = table["D3"]
>>> print(cell, cell.value)
WriteTableCell(cell_name=C4) Sean Connery

See also

__init__(owner, component, lo_inst=None)[source]

Constructor

Parameters:
  • owner (T) – Owner of this component.

  • component (XTextTable) – UNO object that supports com.sun.star.text.TextContent service.

  • lo_inst (LoInst, optional) – Lo instance. Defaults to None.

Return type:

None

add_event_chart_data_change_event_events_disposing(cb)

Adds a listener for an event.

Event is invoked when the broadcaster is about to be disposed.

The callback EventArgs.event_data will contain a UNO com.sun.star.lang.EventObject struct.

Return type:

None

Parameters:

cb (Any) –

add_event_chart_data_changed(cb)

Adds a listener for an event.

Event is invoked when chart data changes in value or structure.

The callback EventArgs.event_data will contain a UNO com.sun.star.chart.ChartDataChangeEvent struct.

Return type:

None

Parameters:

cb (Any) –

add_event_observers(*args)

Adds observers that gets their trigger method called when this class trigger method is called.

Parameters:

args (EventObserver) – One or more observers to add.

Return type:

None

Note

Observers are removed automatically when they are out of scope.

add_event_property_change(name, cb)

Adds a listener for an event.

Event is invoked when property is changed.

The callback EventArgs.event_data will contain a com.sun.star.beans.PropertyChangeEvent struct.

Parameters:
  • name (str) – Property Name

  • cb (EventArgsCallbackT) – Callback

Return type:

None

add_event_property_change_events_disposing(name, cb)

Adds a listener for an event.

Event is invoked when the property listener is about to be disposed.

The callback EventArgs.event_data will contain a UNO com.sun.star.lang.EventObject struct.

Parameters:
  • name (str) – Property Name

  • cb (EventArgsCallbackT) – Callback

Return type:

None

add_event_vetoable_change(name, cb)

Adds a listener for an event.

Event is invoked when property is changed.

The callback EventArgs.event_data will contain a com.sun.star.beans.PropertyChangeEvent struct.

Parameters:
  • name (str) – Property Name

  • cb (EventArgsCallbackT) – Callback

Return type:

None

add_event_vetoable_change_events_disposing(name, cb)

Adds a listener for an event.

Event is invoked when the property listener is about to be disposed.

The callback EventArgs.event_data will contain a UNO com.sun.star.lang.EventObject struct.

Parameters:
  • name (str) – Property Name

  • cb (EventArgsCallbackT) – Callback

Return type:

None

apply_styles(*styles, **kwargs)

Applies style to component.

Parameters:
  • obj. (styles expandable list of styles object such as Font to apply to) –

  • kwargs (Any, optional) – Expandable list of key value pairs.

  • styles (StyleT) –

Return type:

None

attach(text_range)

Attaches a text range to this text content.

Return type:

None

Parameters:

text_range (XTextRange) –

auto_format(name)

Applies an AutoFormat to the cell range of the current context.

Parameters:

name (str) – The name of the AutoFormat to be applied.

Raises:

com.sun.star.lang.IllegalArgumentExceptionIllegalArgumentException

Return type:

None

create_cursor_by_cell_name(name)[source]

Creates a text table cursor and returns the XTextTableCursor interface.

Initially the cursor is positioned in the cell with the specified name.

Return type:

WriteTextTableCursor

Parameters:

name (str) –

ensure_colum_row(cell_obj: ooodev.utils.data_type.cell_obj.CellObj)[source]
ensure_colum_row(col: int, row: int)
ensure_colum_row(addr: com.sun.star.table.CellAddress)
ensure_colum_row(cell: com.sun.star.table.XCell)
ensure_colum_row(val: ooodev.utils.data_type.cell_values.CellValues)
ensure_colum_row(name: str)
ensure_colum_row(*args, **kwargs)

Ensures that the table has at least the specified number of columns and rows.

Returns:

None

Return type:

None

ensure_column_count(count)[source]

Ensures that the table has at least the specified number of columns.

Parameters:

count (int) – Number of columns.

Returns:

Number of columns.

Return type:

int

ensure_row_count(count)[source]

Ensures that the table has at least the specified number of rows.

Parameters:

count (int) – Number of rows.

Returns:

Number of rows.

Return type:

int

get_anchor()

Returns the anchor of this text content.

Return type:

XTextRange

get_cell(cell_obj: ooodev.utils.data_type.cell_obj.CellObj)[source]
get_cell(values: Tuple[int, int])
get_cell(col: int, row: int)
get_cell(addr: com.sun.star.table.CellAddress)
get_cell(cell: com.sun.star.table.XCell)
get_cell(val: ooodev.utils.data_type.cell_values.CellValues)
get_cell(name: str)
get_cell(*args, **kwargs)

Returns a single cell within the range.

Return type:

WriteTableCell

get_cell_by_name(name)[source]

Returns the cell with the specified name.

The cell in the 4th column and third row has the name D3.

Parameters:

name (str) – The name of the cell.

Returns:

The cell with the specified name.

Return type:

WriteTableCell

Note

In cells that are split, the naming convention is more complex. In this case the name is a concatenation of the former cell name (i.e. D3) and the number of the new column and row index inside of the original table cell separated by dots. This is done recursively.

For example, if the cell D3 is horizontally split, it now contains the cells D3.1.1 and D3.1.2.

get_cell_by_position(column, row)[source]

Returns a single cell within the range.

Raises:

com.sun.star.lang.IndexOutOfBoundsExceptionIndexOutOfBoundsException

Return type:

WriteTableCell

Parameters:
  • column (int) –

  • row (int) –

get_cell_names()

Gets the names of all cells in the table.

Return type:

Tuple[str, ...]

get_cell_range(cell_obj: ooodev.utils.data_type.cell_obj.CellObj)[source]
get_cell_range(range_obj: ooodev.utils.data_type.range_obj.RangeObj)
get_cell_range(col_start: int, row_start: int, col_end: int)
get_cell_range(addr: com.sun.star.table.CellRangeAddress)
get_cell_range(rng: ooodev.utils.data_type.range_values.RangeValues)
get_cell_range(rng_name: str)
get_cell_range(*args, **kwargs)

Gets a range Object representing a range.

Parameters:

rng_name (str) – Range as string such as Sheet1.A1:C125 or A1:C125

Returns:

Range object.

Return type:

WriteTableCellRange

get_cell_range_by_name(rng)[source]

Returns a sub-range of cells within the range.

The sub-range is specified by its name. The format of the range name is dependent of the context of the table. In spreadsheets valid names may be A1:C5 or $B$2 or even defined names for cell ranges such as MySpecialCell.

Return type:

WriteTableCellRange

Parameters:

rng (str) –

get_cell_range_by_position(left, top, right, bottom)[source]

Returns a sub-range of cells within the range.

Raises:

com.sun.star.lang.IndexOutOfBoundsExceptionIndexOutOfBoundsException

Return type:

WriteTableCellRange

Parameters:
  • left (int) –

  • top (int) –

  • right (int) –

  • bottom (int) –

get_cell_value(cell, formula_value=False)[source]

Get the cell value.

Parameters:
  • cell (XCell) – Cell to get the value from.

  • formula_value (bool, optional) – Specifies if formula value should be returned instead of the formula. Defaults to False.

Returns:

Cell value.

Return type:

float | str | None

Note

  • If the cell content is a float then a float is returned.

  • If the cell content is a string or a formula then a string is returned.

  • If the cell content is empty then None is returned.

If the cell has a formula set then if formula_value is False then the formula is returned; Otherwise, the result of the formula is returned.

get_columns()[source]

Gets the columns of the table.

Return type:

WriteTableColumns

get_data_array()

Gets an array from the contents of the cell range.

Each element of the result contains a float or a string.

Return type:

Tuple[Tuple[Any, ...], ...]

get_name()

Returns the name of the object.

Return type:

str

get_rows()[source]

Gets the rows of this table.

Returns:

Table Rows

Return type:

WriteTableRows

get_table_range()[source]

Gets a range object from a range string.

Parameters:

rng (str) – Range string.

Returns:

Range Object.

Return type:

RangeObj

initialize(rows, columns)

Determines the numbers of rows and columns of the text table.

This method must be called after the object is created and before the object is insert or attached elsewhere.

Return type:

None

Parameters:
  • rows (int) –

  • columns (int) –

qi(atype, raise_err=False)

Generic method that get an interface instance from an object.

Parameters:
  • atype (T) – Interface type to query obj for. Any Uno class that starts with ‘X’ such as XInterface

  • raise_err (bool, optional) – If True then raises MissingInterfaceError if result is None. Default False

Raises:

MissingInterfaceError – If ‘raise_err’ is ‘True’ and result is None

Returns:

instance of interface if supported; Otherwise, None

Return type:

T | None

Note

When raise_err=True return value will never be None.

remove_event_chart_data_change_event_events_disposing(cb)

Removes a listener for an event

Return type:

None

Parameters:

cb (Any) –

remove_event_chart_data_changed(cb)

Removes a listener for an event

Return type:

None

Parameters:

cb (Any) –

remove_event_observer(observer)

Removes an observer

Parameters:

observer (EventObserver) – One or more observers to add.

Returns:

True if observer has been removed; Otherwise, False.

Return type:

bool

remove_event_property_change(name)

Removes a listener for an event

Parameters:

name (str) – Property Name

Return type:

None

remove_event_property_change_events_disposing(name)

Removes a listener for an event

Parameters:

name (str) – Property Name

Return type:

None

remove_event_vetoable_change(name)

Removes a listener for an event

Parameters:

name (str) – Property Name

Return type:

None

remove_event_vetoable_change_events_disposing(name)

Removes a listener for an event

Parameters:

name (str) – Property Name

Return type:

None

set_data_array(array)

Fills the cell range with values from an array.

The size of the array must be the same as the size of the cell range. Each element of the array must contain a float or a string.

Warning

The size of the array must be the same as the size of the cell range. This means when setting table data the table must be the same size as the data. When setting a table range the array must be the same size as the range.

Return type:

None

Parameters:

array (Sequence[Sequence[Any]]) –

set_data_array_cell(data, cell, ensure_rows=True, ensure_cols=False)[source]

Sets the data array to the table starting from the specified cell.

Parameters:
  • data (Sequence[Sequence[Any]]) – 2D Data array representing the table.

  • cell (str | CellObj) – Cell name or Cell Object.

  • ensure_rows (bool, optional) – Specifies if rows should be added if there are not enough in the table to allow the data to be added. Defaults to True.

  • ensure_cols (bool, optional) – Specifies if columns should be added if there are not enough in the table to allow the data to be added. Defaults to False.

Return type:

None

Example

In this example there are 4 columns and 25 rows in the table. The data array is 4x25. After adding the data array to the table, the table will have 5 columns and 26 rows. This is because we started the data array from the cell B2. By setting ensure_rows to True and ensure_cols to True, the table will have enough rows and columns to accommodate the data array.

>>> cursor = doc.get_cursor()
# add a table with a single cell empty cell
>>> _ = cursor.add_table(table_data=[[[]]], first_row_header=False)
>>> tbl.set_data_array_cell(tbl_data, cell="B2", ensure_cols=True, ensure_rows=True)
>>> print(len(tbl.rows))
26
>>> print(len(tbl.columns))
5
>>> cell = tbl["E24"]
>>> print(cell.value)
"Marc Forster"
set_name(name)

Sets the name of the object.

Return type:

None

Parameters:

name (str) –

subscribe_event(event_name, callback)

Add an event listener to current instance.

Parameters:
  • event_name (str) – Event Name.

  • callback (EventCallback) – Callback of the event listener.

Return type:

None

trigger_event(event_name, event_args)

Trigger an event on current instance.

Parameters:
  • event_name (str) – Event Name.

  • event_args (EventArgsT) – Event Args.

Return type:

None

unsubscribe_event(event_name, callback)

Remove an event listener from current instance.

Parameters:
  • event_name (str) – Event Name.

  • callback (EventCallback) – Callback of the event listener.

Return type:

None

property anchor_type: TextContentAnchorType

Gets/Sets the anchor type of this text content.

Returns:

Anchor Type

Return type:

TextContentAnchorType

Hint

  • TextContentAnchorType``can be imported from ``ooo.dyn.text.text_content_anchor_type.

property back_color: Color

Gets/Sets the color of the background.

Returns:

Color

Return type:

Color

property back_graphic: XGraphic | None

Gets/Sets the graphic for the background.

optional

Return type:

XGraphic | None

property back_graphic_filter: str

Gets/Sets the name of the file filter for the background graphic.

Return type:

str

property back_graphic_location: GraphicLocation

Gets/Sets the position of the background graphic.

Returns:

Graphic Location

Return type:

GraphicLocation

Hint

  • GraphicLocation can be imported from ooo.dyn.style.graphic_location.

property back_graphic_url: str

Gets/Sets the URL for the background graphic.

Note the new behavior since it this was deprecated: This property can only be set and only external URLs are supported (no more vnd.sun.star.GraphicObject scheme). When an URL is set, then it will load the graphic and set the BackGraphic property.

Return type:

str

property back_transparent: bool

Gets/Sets if the background color is transparent.

Return type:

bool

property bottom_margin: int

Gets/Sets the bottom margin.

Return type:

int

property break_type: BreakType | None

Gets/Sets the type of break that is applied at the beginning of the table.

optional

Returns:

Break Type

Return type:

BreakType

Hint

  • BreakType can be imported from ooo.dyn.style.break_type

property chart_column_as_label: bool

Gets/Sets if the first column of the table should be treated as axis labels when a chart is to be created.

Return type:

bool

property chart_row_as_label: bool

Gets/Sets if the first row of the table should be treated as axis labels when a chart is to be created.

Return type:

bool

property collapsing_borders: bool | None

Gets/Sets whether borders of neighboring table cells are collapsed into one.

optional

Return type:

bool | None

property columns: WriteTableColumns

Table Rows

Return type:

WriteTableColumns

property component: com.sun.star.text.TextContent

Sheet Cell Cursor Component

Return type:

TextContent

property event_observer: EventObserver

Gets/Sets The Event Observer for this instance.

Return type:

EventObserver

property events_listener_chart_data_change_event: ChartDataChangeEventListener

Returns listener

Return type:

ChartDataChangeEventListener

property header_row_count: int | None

Gets/Sets the number of rows of the table repeated on every new page.

optional

Return type:

int | None

property hori_orient: HoriOrientationEnum

Gets/Sets the horizontal orientation.

When setting the value can be an integer or a HoriOrientationEnum.

Returns:

Horizontal Orientation

Return type:

HoriOrientationEnum

Hint

  • HoriOrientationEnum can be imported from ooo.dyn.text.hori_orientation

property is_width_relative: bool

Gets/Sets if the value of the relative width is valid.

Return type:

bool

property keep_together: bool

Gets/Sets - Setting this property to True prevents page or column breaks between this table and the following paragraph or text table.

Return type:

bool

property left_margin: int

Gets/Sets the left margin of the table.

Return type:

int

property lo_inst: LoInst

Lo Instance

Return type:

LoInst

property name: str

Get/Sets the name of the table.

When setting the name, it will be converted to a valid name by replacing spaces with underscores and removing leading and trailing spaces.

Return type:

str

property office_doc: OfficeDocumentT

Office Document.

Return type:

OfficeDocumentT

property owner: T

Owner of this component.

Return type:

TypeVar(T, bound= ComponentT)

property page_desc_name: str

Gets/Sets - If this property is set, it creates a page break before the table and assigns the value as the name of the new page style sheet to use.

Return type:

str

property page_number_offset: int

Gets/Sets - If a page break property is set at the table, this property contains the new value for the page number.

Return type:

int

property range_converter: RangeConverter

Gets access to a range converter.

Return type:

RangeConverter

property relative_width: int

Gets/Sets the width of the table relative to its environment.

Return type:

int

property repeat_headline: bool

Gets/Sets if the first row of the table is repeated on every new page.

Return type:

bool

property right_margin: int

Gets/Sets the right margin of the table.

Return type:

int

property rows: WriteTableRows

Table Rows

Return type:

WriteTableRows

property shadow_format: ShadowFormatStructComp

Gets/Sets the type, color and size of the shadow.

When setting the value can be an instance of ShadowFormatStructComp or ShadowFormat.

Returns:

Shadow Format

Return type:

ShadowFormatStructComp

Hint

  • ShadowFormat can be imported from ooo.dyn.table.shadow_format

property split: bool

Get/Sets - Setting this property to False prevents the table from getting spread on two pages.

Return type:

bool

property style_direct: TableStyler

Direct Cell Styler.

Returns:

Character Styler

Return type:

CellStyler

property table_border: TableBorderStructComp

Gets/Sets a description of the cell or cell range border.

If used with a cell range, the top, left, right, and bottom lines are at the edges of the entire range, not at the edges of the individual cell.

Setting value can be done with a TableBorder or TableBorderStructComp object.

Returns:

Table Border.

Return type:

TableBorderComp

Hint

  • TableBorder can be imported from ooo.dyn.table.table_border

property table_border2: TableBorder2StructComp

Gets/Sets a description of the cell or cell range border.

If used with a cell range, the top, left, right, and bottom lines are at the edges of the entire range, not at the edges of the individual cell.

Setting value can be done with a TableBorder2 or TableBorder2StructComp object.

Returns:

Table Border.

Return type:

TableBorder2StructComp

Hint

  • TableBorder2 can be imported from ooo.dyn.table.table_border2

property table_column_relative_sum: int

Gets the sum of the relative widths of all columns.

Return type:

int

property table_column_separators: TableColumnSeparators

Table Column Separators

Moving a column separator will change the width of the column to the left and right of the separator. There is one less separator than there are columns.

To get the number of separators, use the length of this property (len(instance.table_column_separators)).

The position of each separator must be greater then the previous separator and less then the next separator. The position of the last separator must be less then the value of table_column_relative_sum property.

Example

table = doc.tables[0]
table.table_column_separators[1].position = 5312

for sep in table.table_column_separators:
    print(sep)
Return type:

TableColumnSeparators

property table_interop_grab_bag: Tuple[PropertyValue, ...] | None

Gets/Sets - Grab bag of table properties, used as a string-any map for interim interop purposes.

This property is intentionally not handled by the ODF filter. Any member that should be handled there should be first moved out from this grab bag to a separate property.

optional

Return type:

Tuple[PropertyValue, …] | None

property table_template_name: str | None

Gets/Sets the name of table style used by the table.

optional

Return type:

str | None

property text_wrap: WrapTextMode

Gets/Sets if the text content is a shape and how the text is wrapped around the shape.

Returns:

Text Wrap Mode

Return type:

WrapTextMode

Hint

  • WrapTextMode can be imported from ooo.dyn.text.wrap_text_mode

property top_margin: int

Gets/Sets the top margin.

Return type:

int

property width: int

Gets/Sets the absolute table width.

As this is only a describing property the value of the actual table may vary depending on the environment the table is located in and the settings of LeftMargin, RightMargin and HoriOrient.

Return type:

int

property write_doc: WriteDoc

Write Document.

Return type:

WriteDoc

property write_table: WriteTable[Any]

Write Document.

Return type:

WriteTable[Any]