Python API

Tables

class RPA.Tables.Dialect

Bases: enum.Enum

CSV dialect

Excel = 'excel'
ExcelTab = 'excel-tab'
Unix = 'unix'
class RPA.Tables.Table(data: Union[Dict[Union[int, str], Union[Dict, List, Tuple, NamedTuple, set]], List[Union[Dict, List, Tuple, NamedTuple, set]], Table, None] = None, columns: Optional[List[str]] = None)

Bases: object

Container class for tabular data.

Supported data formats:

  • empty: None values populated according to columns/index

  • list: list of data Rows

  • dict: Dictionary of columns as keys and Rows as values

  • table: An existing Table

Row: a namedtuple, dictionary, list or a tuple

Parameters
  • data – Values for table, see “Supported data formats”

  • columns – Names for columns, should match data dimensions

append_column(column=None, values=None)
append_row(row=None)

Append new row to table.

append_rows(rows)

Append multiple rows to table.

append_table(table)

Append data from table to current data.

clear()

Remove all rows from this table.

column_location(value)

Find location for column value.

property columns
copy()

Create a copy of this table.

property data
delete_columns(columns)

Remove columns with matching names.

delete_rows(indexes)

Remove rows with matching indexes.

property dimensions
filter_by_column(column, condition)

Remove rows by evaluating condition for all column values. All rows where it evaluates to falsy are removed.

The filtering will be done in-place.

get(indexes=None, columns=None, as_list=False)

Get values from table. Return type depends on input dimensions.

If indexes and columns are scalar, i.e. not lists:

Returns single cell value

If either indexes or columns is a list:

Returns matching row or column

If both indexes and columns are lists:

Returns a new Table instance with matching cell values

Parameters
  • indexes – List of indexes, or all if not given

  • columns – List of columns, or all if not given

get_cell(index, column)

Get single cell value.

get_column(column, indexes=None, as_list=False)

Get row values from column.

Parameters
  • columns – Name for column

  • indexes – Row indexes to include, or all if not given

  • as_list – Return column as dictionary, instead of list

get_row(index, columns=None, as_list=False)

Get column values from row.

Parameters
  • index – Index for row

  • columns – Column names to include, or all if not given

  • as_list – Return row as dictionary, instead of list

get_slice(start=None, end=None)

Get a new table from rows between start and end index.

get_table(indexes=None, columns=None, as_list=False)

Get a new table from all cells matching indexes and columns.

group_by_column(column)

Group rows by column value and return as list of tables.

head(rows, as_list=False)

Return first n rows of table.

property index
index_location(value)
iter_dicts(with_index=True) → Generator[Dict[Union[int, str], Any], None, None]

Iterate rows with values as dicts.

iter_lists(with_index=True)

Iterate rows with values as lists.

iter_tuples(with_index=True, name='Row')

Iterate rows with values as namedtuples. Converts column names to valid Python identifiers, e.g. “First Name” -> “First_Name”

set(indexes=None, columns=None, values=None)

Sets multiple cell values at a time.

Both indexes and columns can be scalar or list-like, which enables setting individual cells, rows/columns, or regions.

If values is scalar, all matching cells will be set to that value. Otherwise the length should match the cell count defined by the other parameters.

set_cell(index, column, value)

Set individual cell value. If either index or column is missing, they are created.

set_column(column, values)

Set values in column. If column is missing, it is created.

set_row(index, values)

Set values in row. If index is missing, it is created.

property size
sort_by_column(columns, ascending=False)

Sort table by columns.

tail(rows, as_list=False)

Return last n rows of table.

to_dict(with_index=True)

Convert table to dict representation.

to_list(with_index=True)

Convert table to list representation.

class RPA.Tables.Tables

Bases: object

Tables is a library for manipulating tabular data inside Robot Framework.

It can import data from various sources and apply different operations to it. Common use-cases are reading and writing CSV files, inspecting files in directories, or running tasks using existing Excel data.

Import types

The data a table can be created from can be of two main types:

  1. An iterable of individual rows, like a list of lists, or list of dictionaries

  2. A dictionary of columns, where each dictionary value is a list of values

For instance, these two input values:

data1 = [
    {"name": "Mark", "age": 58},
    {"name": "John", "age": 22},
    {"name": "Adam", "age": 67},
]

data2 = {
    "name": ["Mark", "John", "Adam"],
    "age":  [    58,     22,     67],
}

Would both result in the following table:

Index

Name

Age

0

Mark

58

1

John

22

2

Adam

67

Indexing columns and rows

Columns can be referred to in two ways: either with a unique string name or their position as an integer. Columns can be named either when the table is created, or they can be (re)named dynamically with keywords. The integer position can always be used, and it starts from zero.

For instance, a table with columns “Name”, “Age”, and “Address” would allow referring to the “Age” column with either the name “Age” or the number 1.

Rows do not have a name, but instead only have an integer index. This index also starts from zero. Keywords where rows are indexed also support negative values, which start counting backwards from the end.

For instance, in a table with five rows, the first row could be referred to with the number 0. The last row could be accessed with either 4 or -1.

Examples

Robot Framework

The Tables library can load tabular data from various other libraries and manipulate it inside Robot Framework.

*** Settings ***
Library    RPA.Tables

*** Keywords ***
Files to Table
    ${files}=    List files in directory    ${CURDIR}
    ${files}=    Create table    ${files}
    Filter table by column    ${files}    size  >=  ${1024}
    FOR    ${file}    IN    @{files}
        Log    ${file}[name]
    END
    Write table to CSV    ${files}    ${OUTPUT_DIR}${/}files.csv

Python

The library is also available directly through Python, where it is easier to handle multiple different tables or do more bespoke manipulation operations.

from RPA.Tables import Tables

library = Tables()
orders = library.read_table_from_csv(
    "orders.csv", columns=["name", "mail", "product"]
)

customers = library.group_table_by_column(rows, "mail")
for customer in customers:
    for order in customer:
        add_cart(order)
    make_order()
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
add_table_column(table: RPA.Tables.Table, name: Optional[str] = None, values: Any = None)

Append a column to a table.

Parameters
  • table – Table to modify

  • name – Name of new column

  • values – Value(s) for new column

The values can either be a list of values, one for each row, or one single value that is set for all rows.

Examples:

# Add empty column
Add table column    ${table}

# Add empty column with name
Add table column    ${table}    name=Home Address

# Add new column where every every row has the same value
Add table column    ${table}    name=TOS    values=${FALSE}

# Add new column where every row has a unique value
${is_first}=    Create list    ${TRUE}    ${FALSE}    ${FALSE}
Add table column    ${table}    name=IsFirst    values=${is_first}
add_table_row(table: RPA.Tables.Table, values: Any = None)

Append rows to a table.

Parameters
  • table – Table to modify

  • values – Value(s) for new row

The values can either be a list of values, or a dictionary where the keys match current column names. Values for unknown keys are discarded.

It can also be a single value that is set for all columns, which is None by default.

Example:s

# Add empty row
Add table row    ${table}

# Add row where every column has the same value
Add table row    ${table}    Unknown

# Add values per column
${values}=    Create dictionary    Username=Mark    Mail=mark@robocorp.com
Add table row    ${table}    ${values}
clear_table(table: RPA.Tables.Table)

Clear table in-place, but keep columns.

Parameters

table – Table to clear

copy_table(table: RPA.Tables.Table) → RPA.Tables.Table

Make a copy of a table object.

Parameters

table – Table to copy

create_table(data: Union[Dict[Union[int, str], Union[Dict, List, Tuple, NamedTuple, set]], List[Union[Dict, List, Tuple, NamedTuple, set]], Table, None] = None, trim: bool = False, columns: List[str] = None) → RPA.Tables.Table

Create Table object from data.

Data can be a combination of various iterable containers, e.g. list of lists, list of dicts, dict of lists.

Parameters
  • data – Source data for table

  • trim – Remove all empty rows from the end of the worksheet, default False

  • columns – Names of columns (optional)

See the main library documentation for more information about supported data types.

export_table(table: RPA.Tables.Table, with_index: bool = False, as_list: bool = True) → Union[List, Dict]

Convert a table object into standard Python containers.

Parameters
  • table – Table to convert to dict

  • with_index – Include index in values

  • as_list – Export data as list instead of dict

Example:

${orders}=       Read worksheet as table    orders.xlsx
Sort table by column    ${orders}    CustomerId
${export}=       Export table    ${orders}
# The following keyword expects a dictionary:
Write as JSON    ${export}
filter_empty_rows(table: RPA.Tables.Table)

Remove all rows from a table which have only None values.

Parameters

table – Table to filter

The filtering will be done in-place.

Example:

${table}=    Read worksheet as table    orders.xlsx
Filter empty rows    ${table}
filter_table_by_column(table: RPA.Tables.Table, column: Union[int, str], operator: str, value: Any)

Remove all rows where column values don’t match the given condition.

Parameters
  • table – Table to filter

  • column – Column to filter with

  • operator – Filtering operator, e.g. >, <, ==, contains

  • value – Value to compare column to (using operator)

See the keyword Find table rows for all supported operators and their descriptions.

The filtering will be done in-place.

Examples:

# Only accept prices that are non-zero
Filter table by column    ${table}   price  !=  ${0}

# Remove uwnanted product types
@{types}=    Create list    Unknown    Removed
Filter table by column    ${table}   product_type  not in  ${types}
find_table_rows(table: RPA.Tables.Table, column: Union[int, str], operator: str, value: Any)

Find all rows in a table which match a condition for a given column.

Parameters
  • table – Table to find from

  • column – Name of column to search

  • operator – Comparison operator

  • value – Value to compare against

Supported operators:

Operator

Description

>

Cell value is larger than

<

Cell value is smaller than

>=

Cell value is larger or equal than

<=

Cell value is smaller or equal than

==

Cell value is equal to

!=

Cell value is not equal to

is

Cell value is the same object

not is

Cell value is not the same object

contains

Cell value contains given value

not contains

Cell value does not contain given value

in

Cell value is in given value

not in

Cell value is not in given value

Returns the matches as a new Table instance.

Examples:

# Find all rows where price is over 200
@{rows}=    Find table rows    Price  >  ${200}

# Find all rows where the status does not contain "removed"
@{rows}=    Find table rows    Status    not contains    removed
get_table_cell(table: RPA.Tables.Table, row: int, column: Union[int, str]) → Any

Get a cell value from a table.

Parameters
  • table – Table to read from

  • row – Row of cell

  • column – Column of cell

Examples:

# Get the value in the first row and first column
Get table cell    ${table}    0    0

# Get the value in the last row and first column
Get table cell    ${table}   -1    0

# Get the value in the third row and column "Name"
Get table cell    ${table}    2    Name
get_table_column(table: RPA.Tables.Table, column: Union[int, str]) → List

Get all values for a single column in a table.

Parameters
  • table – Table to read

  • column – Column to read

Example:

${emails}=    Get table column    ${users}    E-Mail Address
FOR    ${email}    IN    @{emails}
    Send promotion    ${email}
END
get_table_dimensions(table: RPA.Tables.Table) → Tuple[int, int]

Return table dimensions, as (rows, columns).

Parameters

table – Table to inspect

Examples:

${rows}  ${columns}=    Get table dimensions    ${table}
Log    Table has ${rows} rows and ${columns} columns.
get_table_row(table: RPA.Tables.Table, row: int, as_list: bool = False) → Union[Dict, List]

Get a single row from a table.

Parameters
  • table – Table to read

  • row – Row to read

  • as_list – Return list instead of dictionary

Examples:

${first}=    Get table row    ${orders}
Log     Handling order: ${first}[Order ID]

${row}=      Get table row    ${data}    -1    as_list=${TRUE}
FOR    ${value}    IN    @{row}
    Log    Data point: ${value}
END
get_table_slice(table: RPA.Tables.Table, start: Optional[int] = None, end: Optional[int] = None) → Union[RPA.Tables.Table, List[List]]

Return a new Table from a range of given Table rows.

Parameters
  • table – Table to read from

  • start – Start index (inclusive)

  • start – End index (exclusive)

If start is not defined, starts from the first row. If end is not defined, stops at the last row.

Examples:

# Get all rows except first five
${slice}=    Get table slice    ${table}    start=5

# Get rows at indexes 5, 6, 7, 8, and 9
${slice}=    Get table slice    ${table}    start=5    end=10

# Get all rows except last five
${slice}=    Get table slice    ${table}    end=-5
group_table_by_column(table: RPA.Tables.Table, column: Union[int, str]) → List[RPA.Tables.Table]

Group a table by column and return a list of grouped Tables.

Parameters
  • table – Table to use for grouping

  • column – Column which is used as grouping criteria

Example:

${orders}=    Read worksheet as table    orders.xlsx
@{groups}=    Group table by column    ${orders}    customer
FOR    ${group}    IN    @{groups}
    # Process all orders for the customer at once
    Process order    ${group}
END
merge_tables(*tables: RPA.Tables.Table, index: Optional[str] = None) → RPA.Tables.Table

Create a union of two tables and their contents.

Parameters
  • tables – Tables to merge

  • index – Column name to use as index for merge

By default rows from all tables are appended one after the other. Optionally a column name can be given with index, which is used to merge rows together.

Example:

For instance, a name column could be used to identify unique rows and the merge operation should overwrite values instead of appending multiple copies of the same name.

Name

Price

Egg

10.0

Cheese

15.0

Ham

20.0

Name

Stock

Egg

12.0

Cheese

99.0

Ham

0.0

${products}=    Merge tables    ${prices}    ${stock}    index=Name
FOR    ${product}    IN    @{products}
    Log many
    ...    Product: ${product}[Name]
    ...    Price: ${product}[Price]
    ...    Stock: ${product}[Stock]
END
pop_table_column(table: RPA.Tables.Table, column: Union[int, str, None] = None) → Union[Dict, List]

Remove column from table and return it.

Parameters
  • table – Table to modify

  • column – Column to remove

Examples:

# Remove column from table and discard it
Pop table column    ${users}   userId

# Remove column from table and iterate over it
${ids}=    Pop table column    ${users}    userId
FOR    ${id}    IN    @{ids}
    Log    User id: ${id}
END
pop_table_row(table: RPA.Tables.Table, row: Optional[int] = None, as_list: bool = False) → Union[Dict, List]

Remove row from table and return it.

Parameters
  • table – Table to modify

  • row – Row index, pops first row if none given

  • as_list – Return list instead of dictionary

Examples:

${first}=    Pop table row    ${orders}
Log     Handling order: ${first}[Order ID]

${row}=      Pop table row    ${data}    -1    as_list=${TRUE}
FOR    ${value}    IN    @{row}
    Log    Data point: ${value}
END
read_table_from_csv(path: str, header: Optional[bool] = None, columns: Optional[List[str]] = None, dialect: Optional[RPA.Tables.Dialect] = None, delimiters: Optional[str] = None, column_unknown: str = 'Unknown', encoding: Optional[str] = None) → RPA.Tables.Table

Read a CSV file as a table.

Parameters
  • path – Path to CSV file

  • header – CSV file includes header

  • columns – Names of columns in resulting table

  • dialect – Format of CSV file

  • delimiters – String of possible delimiters

  • column_unknown – Column name for unknown fields

  • encoding – Text encoding for input file, uses system encoding by default

By default attempts to deduce the CSV format and headers from a sample of the input file. If it’s unable to determine the format automatically, the dialect and header will have to be defined manually.

Valid dialect values are excel, excel-tab, and unix, and header is boolean argument (True/False). Optionally a set of valid delimiters can be given as a string.

The columns argument can be used to override the names of columns in the resulting table. The amount of columns must match the input data.

If the source data has a header and rows have more fields than the header defines, the remaining values are put into the column given by column_unknown. By default it has the value “Unknown”.

Examples:

# Source dialect is deduced automatically
${table}=    Read table from CSV    export.csv
Log   Found columns: ${table.columns}

# Source dialect is known and given explicitly
${table}=    Read table from CSV    export-excel.csv    dialect=excel
Log   Found columns: ${table.columns}
rename_table_columns(table: RPA.Tables.Table, names: List[Optional[str]], strict: bool = False)

Renames columns in the Table with given values. Columns with name as None will use the previous value.

Parameters
  • table – Table to modify

  • names – List of new column names

  • strict – If True, raises ValueError if column lengths do not match

The renaming will be done in-place.

Examples:

${columns}=    Create list   First  Second  Third
Rename table columns    ${table}    ${columns}
# First, Second, Third


${columns}=    Create list   Uno  Dos
Rename table columns    ${table}    ${columns}
# Uno, Dos, Third
set_row_as_column_names(table: RPA.Tables.Table, row: int)

Set existing row as names for columns.

Parameters
  • table – Table to modify

  • row – Row to use as column names

Examples:

${table}=    Read table from CSV    data.csv
Set row as column names    ${table}    0
set_table_cell(table: RPA.Tables.Table, row: int, column: Union[int, str], value: Any)

Set a cell value in a table.

Parameters
  • table – Table to modify to

  • row – Row of cell

  • column – Column of cell

  • value – Value to set

Examples:

# Set the value in the first row and first column to "First"
Set table cell    ${table}    0    0       First

# Set the value in the last row and first column to "Last"
Set table cell    ${table}   -1    0       Last

# Set the value in the third row and column "Name" to "Unknown"
Set table cell    ${table}    2    Name    Unknown
set_table_column(table: RPA.Tables.Table, column: Union[int, str], values: Any)

Assign values to entire column in the table.

Parameters
  • table – Table to modify

  • column – Column to modify

  • values – Value(s) to set

The values can either be a list of values, one for each row, or one single value that is set for all rows.

Examples:

# Set different value for each row (sizes must match)
${ids}=    Create list    1  2  3  4  5
Set table column    ${users}    userId    ${ids}

# Set the same value for all rows
Set table column    ${users}    email     ${NONE}
set_table_row(table: RPA.Tables.Table, row: int, values: Any)

Assign values to a row in the table.

Parameters
  • table – Table to modify

  • row – Row to modify

  • values – Value(s) to set

The values can either be a list of values, or a dictionary where the keys match current column names. Values for unknown keys are discarded.

It can also be a single value that is set for all columns.

Examples:

${columns}=  Create list     One  Two  Three
${table}=    Create table    columns=${columns}

${values}=   Create list     1  2  3
Set table row    ${table}    0    ${values}

${values}=   Create dictionary    One=1  Two=2  Three=3
Set table row    ${table}    1    ${values}

Set table row    ${table}    2    ${NONE}
sort_table_by_column(table: RPA.Tables.Table, column: Union[int, str], ascending: bool = True)

Sort a table in-place according to column.

Parameters
  • table – Table to sort

  • column – Column to sort with

  • ascending – Table sort order

Example:

${orders}=    Read worksheet as table    orders.xlsx
Sort table by column    ${orders}    order_date
table_head(table: RPA.Tables.Table, count: int = 5, as_list: bool = False) → Union[RPA.Tables.Table, List[List]]

Return first count rows from a table.

Parameters
  • table – Table to read from

  • count – Number of lines to read

  • as_list – Return list instead of Table

Examples:

# Get the first 10 employees
${employees}=    Read worksheet as table    employees.xlsx
${first}=        Table head    ${employees}    10
table_tail(table: RPA.Tables.Table, count: int = 5, as_list: bool = False) → Union[RPA.Tables.Table, List[List]]

Return last count rows from a table.

Parameters
  • table – Table to read from

  • count – Number of lines to read

  • as_list – Return list instead of Table

Examples:

# Get the last 10 orders
${orders}=    Read worksheet as table    orders.xlsx
${latest}=    Table tail    ${orders}    10
trim_column_names(table: RPA.Tables.Table)

Remove all extraneous whitespace from column names.

Parameters

table – Table to filter

The filtering will be done in-place.

Example:

${table}=    Read table from CSV    data.csv
Log    ${table.columns}  # "One", "Two ", "  Three "
Trim column names     ${table}
Log    ${table-columns}  # "One", "Two", "Three"
trim_empty_rows(table: RPA.Tables.Table)

Remove all rows from the end of a table which have only None as values.

Parameters

table – Table to filter

The filtering will be done in-place.

Example:

${table}=    Read worksheet as table    orders.xlsx
Trim empty rows    ${table}
write_table_to_csv(table: RPA.Tables.Table, path: str, header: bool = True, dialect: RPA.Tables.Dialect = <Dialect.Excel: 'excel'>, encoding: Optional[str] = None)

Write a table as a CSV file.

Parameters
  • table – Table to write

  • path – Path to write to

  • header – Write columns as header to CSV file

  • dialect – The format of output CSV

  • encoding – Text encoding for output file, uses system encoding by default

Valid dialect values are Excel, ExcelTab, and Unix.

Example:

${sheet}=    Read worksheet as table    orders.xlsx    header=${TRUE}
Write table to CSV    ${sheet}    output.csv
RPA.Tables.if_none(value: Any, default: Any)

Return default if value is None.

RPA.Tables.iskeyword()

x.__contains__(y) <==> y in x.

RPA.Tables.to_condition(operator: str, value: Any)

Convert string operator into callable condition function.

RPA.Tables.to_identifier(val: Any)

Convert string to valid identifier

RPA.Tables.to_list(obj: Any, size: int = 1)

Convert (possibly scalar) value to list of size.

RPA.Tables.uniq(seq: Iterable)

Return list of unique values while preserving order. Values must be hashable.