lpod.table

class lpod.table.odf_cell(native_element)

Bases: lpod.element.odf_element

Class for the table cell element.

get_cell_currency()

Get the currency used for monetary values.

get_cell_formula()

Get the formula of the cell, or None if undefined.

The formula is not interpreted in any way.

Return: unicode

get_cell_repeated()

Get the number of times the cell is repeated.

Always None when using the table API.

Return: int or None

get_cell_style()

Get the style of the cell itself.

Return: unicode

get_cell_type()

Get the type of the cell: boolean, float, date, string or time.

Return: str

get_cell_value()

Get the Python value that represent the cell.

Possible return types are unicode, int, Decimal, datetime, timedelta.

Return: Python type

set_cell_currency(currency)

Set the currency used for monetary values.

Arguments:

currency – unicode
set_cell_formula(formula)

Set the formula of the cell, or None to remove it.

The formula is not interpreted in any way.

Arguments:

formula – unicode
set_cell_repeated(repeated)

Set the numnber of times the cell is repeated, or None to delete it.

Arguments:

repeated – int or None
set_cell_style(style)

Set the style of the cell itself.

Arguments:

style – unicode
set_cell_type(cell_type)

Set the type ofthe cell manually.

Arguments:

cell_type – ‘boolean’, ‘float’, ‘date’, ‘string’ or ‘time’
set_cell_value(value, text=None, cell_type=None, currency=None)

Set the cell state from the Python value type.

Text is how the cell is displayed. Cell type is guessed, unless provided.

For monetary values, provide the name of the currency.

Arguments:

value – Python type

text – unicode

cell_type – ‘boolean’, ‘float’, ‘date’, ‘string’ or ‘time’

currency – unicode

class lpod.table.odf_column(native_element)

Bases: lpod.element.odf_element

get_column_default_cell_style()
get_column_repeated()

Get the number of times the column is repeated.

Always None when using the table API.

Return: int or None

get_column_style()
set_column_default_cell_style(style)
set_column_repeated(repeated)

Set the numnber of times the column is repeated, or None to delete it.

Arguments:

repeated – int or None
set_column_style(style)
class lpod.table.odf_row(native_element)

Bases: lpod.element.odf_element

append_cell(cell=None)

Append the given cell at the end of the row. Repeated cells are accepted. If no cell is given, an empty one is created.

Do not use when working on a table, use odf_table.append_cell.

Arguments:

cell – odf_cell
delete_cell(x)

Delete the cell at the given position “x” starting from 0. Alphabetical positions like “D” are accepted.

Cells on the right will be shifted to the left. In a table, other rows remain unaffected.

Arguments:

x – int or str
get_cell(x)

Get the cell at position “x” starting from 0. Alphabetical positions like “D” are accepted.

A copy is returned, use set_cell to push it back.

Arguments:

x – int or str

Return: odf_cell

get_cell_list(style=None, content=None)

Get the list of cells matching the criteria. Each result is a tuple of (x, y, cell).

Arguments:

regex – unicode

style – unicode

Return: list of tuples

get_cell_value(x)

Shortcut to get the value of the cell at position “x”.

See get_cell and odf_cell.get_cell_value.

get_cell_values()

Shortcut to get the list of all cell values in this row.

Return: list of Python types

get_row_repeated()

Get the number of times the row is repeated.

Always None when using the table API.

Return: int or None

get_row_style()

Get the style of the row itself.

Return: unicode

get_row_width()

Get the number of expected cells in the row, i.e. addition repetitions.

Return: int

insert_cell(x, cell=None)

Insert the given cell at position “x” starting from 0. If no cell is given, an empty one is created.

Alphabetical positions like “D” are accepted.

Do not use when working on a table, use odf_table.insert_cell.

Arguments:

x – int or str

cell – odf_cell

is_row_empty(aggressive=False)

Return wether every cell in the row has no value or the value evaluates to False (empty string), and no style.

If aggressive is True, empty cells with style are considered empty.

Arguments:

aggressive – bool

Return: bool

rstrip_row(aggressive=False)

Remove in-place empty cells at the right of the row. Cells are empty if they contain no value or it evaluates to False, and no style.

If aggressive is True, empty cells with style are removed too.

Arguments:

aggressive – bool
set_cell(x, cell=None)

Push the cell back in the row at position “x” starting from 0. Alphabetical positions like “D” are accepted.

Arguments:

x – int or str
set_cell_value(x, value)

Shortcut to set the value of the cell at position “x”.

See get_cell and odf_cell.get_cell_value.

set_cell_values(values)

Shortcut to set the list of all cell values in this row.

Arguments:

values – list of Python types
set_row_repeated(repeated)

Set the numnber of times the row is repeated, or None to delete it.

Arguments:

repeated – int or None
set_row_style(style)

Set the style of the row itself.

Arguments:

style – unicode
traverse_cells()

Yield as many cell elements as expected cells in the row, i.e. expand repetitions by returning the same cell as many times as necessary.

Copies are returned, use set_cell to push them back.

class lpod.table.odf_row_group(native_element)

Bases: lpod.element.odf_element

Class to group rows with common properties.

class lpod.table.odf_table(native_element)

Bases: lpod.element.odf_element

append_cell(y, cell=None)

Append the given cell at the “y” coordinate. Repeated cells are accepted. If no cell is given, an empty one is created.

Position start at 0. So cell A4 is on row 3.

Other rows remain untouched.

Arguments:

y – int

cell – odf_cell

append_column(column=None)

Append the column at the end of the table. If no column is given, an empty one is created.

ODF columns don’t contain cells, only style information.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

column – odf_column
append_row(row=None)

Append the row at the end of the table. If no row is given, an empty one is created.

Position start at 0. So cell A4 is on row 3.

Note the columns are automatically created when the first row is inserted in an empty table. So better insert a filled row.

Arguments:

row – odf_row
delete_cell(coordinates)

Delete the cell at the given coordinates, so that next cells are shifted to the left.

Coordinates are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

Use “set_cell_value” for erasing value.

Arguments:

coordinates – (int, int) or str
delete_column(x)

Delete the column at the given position. ODF columns don’t contain cells, only style information.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

x – int or str.isalpha()
delete_row(y)

Delete the row at the given “y” position.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int
export_to_csv(path_or_file, delimiter=';', quotechar='"', lineterminator='n', encoding='utf-8')

Write the table as CSV in the file. If the file is a string, it is opened as a local path. Else a open file-like is expected; it will not be closed afterwards.

Arguments:

path_or_file – str or file-like

delimiter – str

quotechar – str

lineterminator – str

encoding – str

get_cell(coordinates)

Get the cell at the given coordinates.

They are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

A copy is returned, use set_cell to push it back.

Arguments:

coordinates – (int, int) or str

Return: odf_cell

get_cell_list(style=None, content=None)

Get the list of cells matching the criteria. Each result is a tuple of (x, y, cell).

Arguments:

regex – unicode

style – unicode

Return: list of tuples

get_cell_value(coordinates)

Shortcut to get the Python value of the cell at the given coordinates.

They are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”. Arguments:

coordinates – (int, int) or str

Return: Python type

get_column(x)

Get the column at the given “x” position.

ODF columns don’t contain cells, only style information.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

A copy is returned, use set_column to push it back.

Arguments:

x – int or str.isalpha()

Return: odf_column

get_column_cell_values(x)

Shortcut to get the list of Python values for the cells at the given position.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

x – int or str.isalpha()

Return: list of Python types

get_column_cells(x)

Get the list of cells at the given position.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

x – int or str.isalpha()

Return: list of odf_cell

get_column_list(style=None)

Get the list of columns matching the criteria. Each result is a tuple of (x, column).

The original column elements are returned, with their repetition attribute.

Arguments:

style – unicode

Return: list of tuples

get_formatted_text(context)
get_row(y)

Get the row at the given “y” position.

Position start at 0. So cell A4 is on row 3.

A copy is returned, use set_cell to push it back.

Arguments:

y – int

Return: odf_row

get_row_list(style=None, content=None)

Get the list of rows matching the criteria. Each result is a tuple of (y, row).

The original row elements are returned, with their repetition attribute.

Arguments:

regex – unicode

style – unicode

Return: list of tuples

get_row_values(y)

Shortcut to get the list of Python values for the cells of the row at the given “y” position.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int
get_table_height()

Get the current height of the table.

Return: int

get_table_name()
get_table_print_ranges()
get_table_size()

Shortcut to get the current width and height of the table.

Return: (int, int)

get_table_style()
get_table_values()

Get a matrix of all Python values of the table.

Return: list of lists

get_table_width()

Get the current width of the table, measured on columns.

Rows may have different widths, use the odf_table API to ensure width consistency.

Return: int

insert_cell(coordinates, cell=None)

Insert the given cell at the given coordinates. If no cell is given, an empty one is created.

Coordinates are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

Cells on the right are shifted. Other rows remain untouched.

Arguments:

coordinates – (int, int) or str

cell – odf_cell

insert_column(x, column=None)

Insert the column before the given “x” position. If no column is given, an empty one is created.

ODF columns don’t contain cells, only style information.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

x – int or str.isalpha()

column – odf_column

insert_row(y, row=None)

Insert the row before the given “y” position. It must have the same number of cells. If no row is given, an empty one is created.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int

row – odf_row

is_column_empty(x, aggressive=False)

Wether all the cells at the given position are empty.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted. If aggressive is True, the cells without data are said empty.

Return: bool

is_row_empty(y, aggressive=False)

Wether all the cells of the row at the given “y” position are undefined.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int
is_table_displayed()
is_table_printable()
is_table_protected()
iter_table_values()

Iterate through lines of Python values of the table.

Return: iterator of lists

rstrip_table(aggressive=False)

Remove in-place empty rows below and empty cells at the right of the table. Cells are empty if they contain no value or it evaluates to False, and no style.

If aggressive is True, empty cells with style are removed too.

Argument:

aggressive – bool
set_cell(coordinates, cell=None)

Replace a cell of the table at the given coordinates.

They are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

Arguments:

coordinates – (int, int) or str

cell – odf_cell

set_cell_image(coordinates, image_frame, type=None)

Do all the magic to display an image in the cell at the given coordinates.

They are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

The frame element must contain the expected image position and dimensions.

Image insertion depends on the document type, so the type must be provided or the table element must be already attached to a document.

Arguments:

coordinates – (int, int) or str

image_frame – odf_frame including an image

type – ‘spreadsheet’ or ‘text’

set_cell_value(coordinates, value)

Set the Python value of the cell at the given coordinates.

They are either a 2-uplet of (x, y) starting from 0, or a human-readable position like “C4”.

Arguments:

coordinates – (int, int) or str

value – Python type

set_column(x, column=None)

Replace the column at the given “x” position.

ODF columns don’t contain cells, only style information.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

Arguments:

x – int or str.isalpha()

column – odf_column

set_column_cell_values(x, values)

Shortcut to set the list of Python values of cells at the given position.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

The list must have the same length than the table height.

Arguments:

x – int or str.isalpha()

values – list of Python types

set_column_cells(x, cells)

Set the list of cells at the given position.

Position start at 0. So cell C4 is on column 2. Alphabetical position like “C” is accepted.

The list must have the same length than the table height.

Arguments:

x – int or str.isalpha()

cells – list of odf_cell

set_row(y, row=None)

Replace the row at the given position with the new one. It must have the same number of cells. Repetion of the old row will be adjusted.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int

row – odf_row

set_row_values(y, values)

Shortcut to set the values of all cells of the row at the given “y” position.

Position start at 0. So cell A4 is on row 3.

Arguments:

y – int

values – list of Python types

set_table_displayed(display)
set_table_name(name)
set_table_print_ranges(print_ranges)
set_table_printable(printable)
set_table_protected(protect)
set_table_style(style)
set_table_values(values)

Set all Python values for the whole table.

A list of lists is expected, with as many lists as rows, and as many items in each sublist as cells.

Arguments:

values – list of lists
traverse_columns()

Yield as many column elements as expected columns in the table, i.e. expand repetitions by returning the same column as many times as necessary.

Copies are returned, use set_column to push them back.

traverse_rows()

Yield as many row elements as expected rows in the table, i.e. expand repetitions by returning the same row as many times as necessary.

Copies are returned, use set_row to push them back.

lpod.table.import_from_csv(path_or_file, name, style=None, delimiter=None, quotechar=None, lineterminator=None, encoding='utf-8')

Convert the CSV file to an odf_table. If the file is a string, it is opened as a local path. Else a open file-like is expected; it will not be closed afterwards.

CSV format can be autodetected to a certain limit, but encoding is important.

Arguments:

path_or_file – str or file-like

name – unicode

style – str

delimiter – str

quotechar – str

lineterminator – str

encoding – str

lpod.table.odf_create_cell(value=None, text=None, cell_type=None, currency=None, repeated=None, style=None)

Create a cell element containing the given value. The textual representation is automatically formatted but can be provided. Cell type can be deduced as well, unless the number is a percentage or currency. If cell type is “currency”, the currency must be given. The cell can be repeated on the given number of columns.

Arguments:

value – bool, int, float, Decimal, date, datetime, str, unicode,
timedelta

text – unicode

cell_type – ‘boolean’, ‘currency’, ‘date’, ‘float’, ‘percentage’,
‘string’ or ‘time’

currency – three-letter str

repeated – int

style – unicode

Return: odf_cell

lpod.table.odf_create_column(default_cell_style=None, repeated=None, style=None)

Create a column group element of the optionally given style. Cell style can be set for the whole column. If the properties apply to several columns, give the number of repeated columns.

Columns don’t contain cells, just style information.

You don’t generally have to create columns by hand, use the odf_table API.

Arguments:

default_cell_style – unicode

repeated – int

style – unicode

Return: odf_column

lpod.table.odf_create_header_rows()
lpod.table.odf_create_row(width=None, repeated=None, style=None)

Create a row element, optionally filled with “width” number of cells.

Rows contain cells, their number determine the number of columns.

You don’t generally have to create rows by hand, use the odf_table API.

Arguments:

width – int

repeated – int

style – unicode

Return: odf_row

lpod.table.odf_create_row_group(height=None, width=None)

Create a group of rows, optionnaly filled with “height” number of rows, of “width” cells each.

Row group bear style information applied to a series of rows.

Arguments:

height – int

Return odf_row_group

lpod.table.odf_create_table(name, width=None, height=None, protected=False, protection_key=None, display=True, printable=True, print_ranges=None, style=None)

Create a table element, optionally prefilled with “height” rows of “width” cells each.

If the table is to be protected, a protection key must be provided, i.e. a hash value of the password (XXX what algorithm?).

If the table must not be displayed, set “display” to False.

If the table must not be printed, set “printable” to False. The table will not be printed when it is not displayed, whatever the value of this argument.

Ranges of cells to print can be provided as a list of cell ranges, e.g. [‘E6:K12’, ‘P6:R12’] or directly as a raw string, e.g. “E6:K12 P6:R12”.

You can access and modify the XML tree manually, but you probably want to use the API to access and alter cells. It will save you from handling repetitions and the same number of cells for each row.

If you use both the table API and the XML API, you are on your own for ensuiring model integrity.

Arguments:

name – unicode

width – int

height – int

protected – bool

protection_key – str

display – bool

printable – bool

print_ranges – list

style – unicode

Return: odf_table