Class TableHelper

class ooodev.utils.table_helper.TableHelper[source]
static col_name_to_int(name, zero_index=False)[source]

Converts a Column Name into an int.

Parameters:
  • name (str) – Case insensitive column name such as ‘a’ or ‘AB’

  • zero_index (bool, optional) – determines if return is zero based or one based. Default False.

Raises:
  • ValueError – If number of Column letters (name) is 0 or greater than 5.

  • ValueError – If name is not valid characters, [a-zA-Z0-9].

Returns:

One based int representing column name

Return type:

int

Example

>>> TableHelper.col_name_to_int('a')
1
>>> TableHelper.col_name_to_int('a', True)
0

Changed in version 0.8.2: Added zero_index parameter.

static convert_1d_to_2d(seq_obj, col_count)[source]
static convert_1d_to_2d(seq_obj, col_count, empty_cell_val)
static convert_1d_to_2d(seq_obj, col_count, empty_cell_val=<object object>)

Converts a 1d sequence into a 2d list.

Parameters:
  • seq_obj (Sequence[T]) – Input sequence

  • col_count (int) – the number of columns to create in the 2d list.

  • empty_cell_val (Any, optional) – When included any columns missing in last row will be added containing this value. None is also an acceptable value.

Raises:

ValueError – If col_count is less then 1.

Returns:

2d list.

Return type:

List[List[T]]

New in version 0.6.7.

classmethod get_cell_parts(cell_name)[source]

Gets cell parts from a cell name.

Parameters:

cell_name (str) – Cell name such as A23 or Sheet1.A23

Returns:

Cell Parts

Return type:

CellParts

Note

If a range name such as A23:G45 or Sheet1.A23:G45 then only the first cell is used.

Column name is upper case.

New in version 0.8.3.

classmethod get_largest_int(seq_obj)[source]

Gets the largest int in a 1d or 2d Sequence integers

Parameters:

seq_obj (Sequence[int] | Sequence[Sequence[int]]) – Input sequence

Raises:

ValueError – If no integers in are found.

Returns:

Largest integer found in sequence

Return type:

int

New in version 0.6.7.

classmethod get_largest_str(seq_obj)[source]

Gets the length of longest string in a 1d or 2d sequence of strings.

Parameters:

seq_obj (Sequence[str] | Sequence[Sequence[str]]) – Input Sequence

Returns:

Length of longest string if found; Otherwise -1

Return type:

int

New in version 0.6.7.

classmethod get_range_parts(range_name)[source]

Gets range parts from a range name.

Parameters:

range_name (str) – Range name such as A23:G45 or Sheet1.A23:G45

Returns:

Range Parts

Return type:

RangeParts

Notes

Column names are upper case.

New in version 0.8.2.

classmethod get_smallest_int(seq_obj)[source]

Gets the smallest int in a 1d or 2d Sequence integers

Parameters:

seq_obj (Sequence[int] | Sequence[Sequence[int]]) – Input sequence

Raises:

ValueError – If no integers in are found.

Returns:

Smallest integer found in sequence

Return type:

int

New in version 0.6.7.

classmethod get_smallest_str(seq_obj)[source]

Gets the length of shortest string in a 1d or 2d sequence of strings.

Parameters:

seq_obj (Sequence[str] | Sequence[Sequence[str]]) – Input Sequence

Returns:

Length of shortest string if found; Otherwise -1

Return type:

int

static make_2d_array(num_rows, num_cols)[source]
static make_2d_array(num_rows, num_cols, val: Any)
static make_2d_array(num_rows, num_cols, val: Callable[[int, int, Any], Any])
static make_2d_array(num_rows, num_cols, val=None)

Make a 2-Dimensional List of values

Parameters:
  • num_rows (int) – Number of rows

  • num_cols (int) – Number of Columns in each row.

  • val (Callable[[int, int, Any], Any]) – Callable that provide each value. Callback e.g. cb(row: int, col: int, prev_value: None | int) -> int:...

Returns:

2-Dimensional List of values

Return type:

List[List[Any]]

Example

Example of array filled with 1’s

arr = TableHelper.make_2d_array(num_rows=3, num_cols=4, val=1)
# arr
# [
#   [1, 1, 1, 1],
#   [1, 1, 1, 1],
#   [1, 1, 1, 1]
# ]

Example of using call back method.

The following example creates an array that loops through each animals and adds to array. When end of animals is reached the start with the beginning of animals and continues in this fashion until array is built.

animals = ("ass", "cat", "cow", "cub", "doe", "dog", "elk",
            "ewe", "fox", "gnu", "hog", "kid", "kit", "man",
            "orc", "pig", "pup", "ram", "rat", "roe", "sow", "yak")
total_rows = 15
total_cols = 6

def cb(row:int, col:int, prev) -> str:
    # return animals repeating until all cells are filled
    v = (row * total_cols) + col

    a_len = len(animals)
    if v > a_len - 1:
        i = (v % a_len)
    else:
        i = v
    return animals[i]

arr = TableHelper.make_2d_array(num_rows=total_rows, num_cols=total_cols, val=cb)
Calc.set_array(values=arr, sheet=sheet, name="A1")
static make_2d_list(num_rows, num_cols, val=None)

Make a 2-Dimensional List of values

Parameters:
  • num_rows (int) – Number of rows

  • num_cols (int) – Number of Columns in each row.

  • val (Callable[[int, int, Any], Any]) – Callable that provide each value. Callback e.g. cb(row: int, col: int, prev_value: None | int) -> int:...

Returns:

2-Dimensional List of values

Return type:

List[List[Any]]

Example

Example of array filled with 1’s

arr = TableHelper.make_2d_array(num_rows=3, num_cols=4, val=1)
# arr
# [
#   [1, 1, 1, 1],
#   [1, 1, 1, 1],
#   [1, 1, 1, 1]
# ]

Example of using call back method.

The following example creates an array that loops through each animals and adds to array. When end of animals is reached the start with the beginning of animals and continues in this fashion until array is built.

animals = ("ass", "cat", "cow", "cub", "doe", "dog", "elk",
            "ewe", "fox", "gnu", "hog", "kid", "kit", "man",
            "orc", "pig", "pup", "ram", "rat", "roe", "sow", "yak")
total_rows = 15
total_cols = 6

def cb(row:int, col:int, prev) -> str:
    # return animals repeating until all cells are filled
    v = (row * total_cols) + col

    a_len = len(animals)
    if v > a_len - 1:
        i = (v % a_len)
    else:
        i = v
    return animals[i]

arr = TableHelper.make_2d_array(num_rows=total_rows, num_cols=total_cols, val=cb)
Calc.set_array(values=arr, sheet=sheet, name="A1")
classmethod make_cell_name(row, col, zero_index=False)[source]

Convert given row and column number to A1 style cell name.

Parameters:
  • row (int) – Row number.

  • col (int) – Column Number.

  • zero_index (bool, optional) – determines if return is zero based or one based. Default False.

Raises:

ValueError – If col Value is <1 for one based or <0 for zero based.

Returns:

row and col as cell name such as A1, AB3

Return type:

str

Example

>>> TableHelper.make_cell_name(1, 1)
A1
>>> TableHelper.make_cell_name(0, 0, True)
A1

Changed in version 0.8.2: Added zero_index parameter.

static make_column_name(col, zero_index=False)[source]

Makes a cell style name. eg: A, B, C, … AA, AB, AC

Parameters:
  • col (int) – Column number.

  • zero_index (bool, optional) – determines if return is zero based or one based. Default False.

Raises:

ValueError – If col Value is <1 for one based or <0 for zero based.

Returns:

column name. eg: A, B, C, … AA, AB, AC

Return type:

str

Changed in version 0.8.2: Added zero_index parameter.

static row_name_to_int(name, zero_index=False)[source]

Converts a row name into an int. Leading Alpha chars are ignore. String 4 converts to integer 4. String C5 converts to integer 5.

If zero_index is True string 4 converts to integer 3. String C5 converts to integer 4.

Parameters:
  • name (str) – row name to convert

  • zero_index (bool, optional) – determines if return is zero based or one based. Default False.

Returns:

converted name as int.

Return type:

int

Example

>>> TableHelper.row_name_to_int('C5')
5
>>> TableHelper.row_name_to_int('C5', True)
4

Changed in version 0.8.2: Added zero_index parameter.

static table_2d_to_dict(tbl)[source]

Converts a 2-D table into a Dictionary Table

Parameters:

tbl (Table) – 2-D table

Raises:

ValueError – If tbl does not contain at least two rows

Returns:

As List of Dict with each dict key representing column name

Return type:

DictTable

static table_dict_to_table(tbl)[source]

Converts a Dictionary Table to a 2-D table

Parameters:

tbl (DictTable) – Dictionary Table

Raises:

ValueError – If tbl does not contain any rows

Returns:

As 2-D table

Return type:

Table

classmethod to_2d_list(seq_obj)[source]

Converts a sequence of sequence to a list.

Converts 1-Dimensional or 2-Dimensional array such as a Tuple or a Tuple of Tuple’s into a List of List.

An array of tuples is immutable and can not add or remove elements whereas a list is mutable.

Parameters:

seq_obj (Sequence[Any]) – 1-Dimensional or 2-Dimensional List

Returns:

2-Dimensional list

Return type:

List[List[Any]]

classmethod to_2d_tuple(seq_obj)[source]

Converts a sequence of sequence to a tuple of tuple.

Converts 1-Dimensional or 2-Dimensional array such as a List or List of list’s into a Tuple of Tuple.

Parameters:

seq_obj (Sequence[Any]) – 1-Dimensional or 2-Dimensional Sequence

Returns:

2-Dimensional tuple

Return type:

Tuple[Tuple[Any, …], …]

static to_list(iter_obj)[source]

Converts an iterable of objects into a list of objects

If iter_obj is not iterable it will be return as a tuple containing iter_obj

Parameters:

iter_obj (Iterable[Any] | object) – iterable object or object.

Returns:

List containing same elements of iter_obj

Return type:

List[Any]

static to_tuple(iter_obj)[source]

Converts an iterable of objects or object into a tuple of objects

If iter_obj is not iterable it will be return as a tuple containing iter_obj

Parameters:

iter_obj (Iterable[Any] | object) – iterable object or object.

Returns:

Tuple containing same elements of iter_obj

Return type:

Tuple[Any]