RPA.Excel.Files
module RPA.Excel.Files
class RPA.Excel.Files.Files
The Excel.Files library can be used to read and write Excel files without the need to start the actual Excel application.
It supports both legacy .xls
files and modern .xlsx
files.
Note: To run macros or load password protected worksheets, please use the Excel application library.
Examples
Robot Framework
A common use-case is to load an existing Excel file as a table, which can be iterated over later in a Robot Framework keyword or task:
Processing all worksheets in the Excel file and checking row count:
Creating a new Excel file with a dictionary:
Creating a new Excel file with a list:
Python
The library can also be imported directly into Python.
variable ROBOT_LIBRARY_DOC_FORMAT
variable ROBOT_LIBRARY_SCOPE
method append_rows_to_worksheet
Append values to the end of the worksheet.
Parameters
- content β Rows of values to append
- name β Name of worksheet to append to (optional). Defaults to the active worksheet.
- header β Set rows according to existing header row
- start β Start of data, NOTE: Only required when header is True
- formatting_as_empty β if True, the cells containing only formatting (no values) are considered empty.
- Returns: List of dictionaries that represents the worksheet
The content
argument can be of any tabular format. Typically,
this is a Table object created by the RPA.Tables
library,
but it can also be a list of lists, or a list of dictionaries.
If the header
flag is enabled, the existing header in the worksheet
is used to insert values in the correct columns. This assumes that
that source data has this data available.
If the header is not on the first row of the worksheet,
the start
argument can be used to give the correct row index.
Examples
method auto_size_columns
Auto size column widths.
Note. non-default font sizes might cause auto sizing issues
Parameters
- start_column β column number or name to start from
- end_column β optional column number or name for last column
- width β if given will resize columns to this size, otherwise will auto_size
Examples
Robot Framework example.
Python example.
method clear_cell_range
Clear cell values for a given range.
- Parameters: range_string β single cell or range of cells
Examples
Robot Framework example.
Python example.
method close_workbook
Close the active workbook.
Examples
method copy_cell_values
Copy cells from source to target.
Parameters
- source_range β single cell or range of cells
- target β copy to this cell
Examples
Robot Framework example.
Python example.
method create_workbook
Create and open a new Excel workbook.
Automatically also creates a new worksheet with the name sheet_name. (defaults to βSheetβ)
Note: Must be paired with the Save Workbook
keyword
or the newly created workbook will be deleted upon robot completion.
Note: The file name/path must be set in either the Create Workbook
keyword or the Save Workbook
keyword and must include the file extension.
Parameters
- path β Save path for workbook; defaults to robot root if not provided.
- fmt β Format of workbook, i.e. xlsx or xls; Defaults to xlsx if not provided.
- sheet_name β Custom name for the initial sheet.
- Returns: Workbook object.
Examples
method create_worksheet
Create a new worksheet in the current workbook.
Parameters
- name β Name of new worksheet
- content β Optional content for worksheet
- exist_ok β If False, raise an error if name is already in use
- header β If content is provided, write headers to worksheet
Examples
method delete_columns
Delete column or columns beginning from start column number/name to possible end column number/name.
Parameters
- start β column number or name to start deletion from
- end β optional column number or name for last column to delete
Examples
Robot Framework example.
Python example.
method delete_rows
Delete row or rows beginning from start row number to possible end row number.
Parameters
- start β row number to start deletion from
- end β optional row number for last row to delete
Examples
Robot Framework example.
Python example.
method find_empty_row
Find the first empty row after existing content, and return the row number.
- Parameters: name β Name of worksheet (optional). Defaults to the active worksheet.
- Returns: First row number of empty row
Examples
method get_active_worksheet
Get the name of the worksheet which is currently active.
- Returns: Active worksheet name
Examples
method get_cell_value
Get a cell value in the given worksheet.
Parameters
- row β Index of row to read, e.g. 3
- column β Name or index of column, e.g. C or 7
- name β Name of worksheet (optional). Defaults to active worksheet.
- Returns: Cell value
Examples
method get_worksheet_value
Alias for keyword Get cell value
, see the original keyword
for documentation.
method hide_columns
Hide column or columns in worksheet.
Parameters
- start_column β column number or name to start from
- end_column β optional column number or name for last column
Examples
Robot Framework example.
Python example.
method insert_columns_after
Insert column or columns after a column number/name.
Parameters
- column β insert after this column
- amount β number of columns to insert, default 1
Examples
Robot Framework example.
Python example.
method insert_columns_before
Insert column or columns before a column number/name.
Parameters
- column β insert before this column
- amount β number of columns to insert, default 1
Examples
Robot Framework example.
Python example.
method insert_image_to_worksheet
Insert an image into the given cell.
The path
argument should be a local file path to the image file.
By default the image is inserted in the original size, but it can
be scaled with the scale
argument. Itβs scaled with a factor
where the value 1.0
is the default.
Parameters
- row β Index of row to write
- column β Name or index of column
- path β Path to image file
- scale β Scale of image (optional). Default value is β1.0β.
- name β Name of worksheet (optional). Defaults to the active worksheet.
Examples
method insert_rows_after
Insert row or rows after a row number.
Parameters
- row β insert after this row
- amount β number of rows to insert, default 1
Examples
Robot Framework example.
Python example.
method insert_rows_before
Insert row or rows before a row number.
Parameters
- row β insert before this row
- amount β number of rows to insert, default 1
Examples
Robot Framework example.
Python example.
method list_worksheets
List all names of worksheets in the given workbook.
- Returns: List containing the names of the worksheets
Examples
method move_range
Move range of cells by given amount of rows and columns.
Formulas are translated to match new location by default.
Note. There is a bug in the openpyxl on moving negative rows/columns.
Parameters
- range_string β cell range
- rows β number of rows to move
- columns β number of columns to move
- translate β are formulas translated for a new location
Examples
Robot Framework example.
Python example.
method open_workbook
Open an existing Excel workbook.
Opens the workbook in memory and sets it as the active workbook. This library can only have one workbook open at a time, and any previously opened workbooks are closed first.
The file can be in either .xlsx
or .xls
format.
Parameters
- path β path to Excel file
- data_only β controls whether cells with formulas have either
the formula (default, False) or the value stored the last time Excel
read the sheet (True). Affects only
.xlsx
files.
- Returns: Workbook object
Examples
method read_worksheet
Read the content of a worksheet into a list of dictionaries.
Each key in the dictionary will be either values from the header row, or Excel-style column letters.
Parameters
- name β Name of worksheet to read (optional). Defaults to the active worksheet.
- header β If True, use the first row of the worksheet as headers for the rest of the rows. Default is False.
- start β Row index to start reading data from (1-indexed). Default value is row 1.
- Returns: List of dictionaries that represents the worksheet
Examples
method read_worksheet_as_table
Read the contents of a worksheet into a Table container. Allows
sorting/filtering/manipulating using the RPA.Tables
library.
Parameters
- name β Name of worksheet to read (optional). Defaults to the active worksheet.
- header β If True, use the first row of the worksheet as headers for the rest of the rows. Default value is False.
- trim β Remove all empty rows from the end of the worksheet. Default value is True.
- start β Row index to start reading data from (1-indexed). Default value is row 1.
- Returns: Table object that represents the worksheet
Examples
method remove_worksheet
Remove a worksheet from the active workbook.
- Parameters: name β Name of worksheet to remove (optional). Defaults to the active worksheet.
Examples
method rename_worksheet
Rename an existing worksheet in the active workbook.
Parameters
- src_name β Current name of worksheet
- dst_name β Future name of worksheet
Examples
method save_workbook
Save the active workbook.
Note: No changes to the workbook are saved to the actual file unless this keyword is called.
- Parameters: path β Path to save to. If not given, uses path given when opened or created.
- Returns: Workbook object
Examples
method set_active_worksheet
Set the active worksheet.
This keyword can be used to set the default worksheet for keywords, which removes the need to specify the worksheet name for each keyword. It can always be overridden on a per-keyword basis.
- Parameters: value β Index or name of worksheet
Examples
method set_cell_format
Set format for cell.
Does not affect the values themselves, but changes how the values are displayed when opening with an external application such as Microsoft Excel or LibreOffice Calc.
Parameters
- row β Index of row to write, e.g. 3
- column β Name or index of column, e.g. C or 7
- fmt β Format code for cell
- name β Name of worksheet (optional). Defaults to active worksheet.
The fmt
argument accepts all format code values that
are supported by the aforementioned applications.
Some examples of valid values:
Format | Explanation |
---|---|
0.00 | Number with two decimal precision |
0% | Percentage without decimals |
MM/DD/YY | Date with month, day, and year |
@ | Text value |
BOOLEAN | Boolean value |
Examples
method set_cell_formula
Set cell formula for given range of cells.
If transpose is set then formula is set for first cell of the range and the rest of cells will transpose the function to match to that cell.
Otherwise (by default) all cells will get the same formula.
Parameters
- range_string β cell range
- formula β formula for the cell
- transpose β on True the cell formulas will be transposed
Examples
Robot Framework example.
Python example.
method set_cell_value
Set a cell value in the given worksheet.
Parameters
- row β Index of row to write, e.g. 3
- column β Name or index of column, e.g. C or 7
- value β New value of cell
- name β Name of worksheet (optional). Defaults to active worksheet.
- fmt β Format code for cell (optional)
Examples
method set_cell_values
Set cell values given as list of lists or as a RPA.Tables.Table.
Note. Will overwrite cells if table structure causes cells to overlap.
Parameters
- start_cell β starting cell in a string
- values β list of lists or a Table
- table_heading β if values are given as a Table, this parameter defines if Table headings should be inserted as a row
Examples
Robot Framework example.
Python example.
method set_styles
Set styles for range of cells.
Possible values for the align_horizontal:
- general
- left
- center
- right
- fill
- justify
- centerContinuous
- distributed
Possible values for the align_vertical:
- top
- center
- bottom
- justify
- distributed
Some examples for number_formats:
Parameters
- range_string β single cell or range of cells
- font_name β name of the font
- family β font family name
- size β size for the font
- bold β font style bold
- italic β font style italics
- underline β font style underline
- strikethrough β font style strikethrough
- cell_fill β cell fill color, in hex or color name
- color β font color, in hex or color name
- align_horizontal β cell horizontal alignment
- align_vertical β cell vertical alignment
- number_format β cell number format
Examples
Robot Framework example.
Python example.
method set_worksheet_value
Alias for keyword Set cell value
, see the original keyword
for documentation.
method unhide_columns
Unhide column or columns in worksheet.
Parameters
- start_column β column number or name to start from
- end_column β optional column number or name for last column
Examples
Robot Framework example.
Python example.
method worksheet_exists
Return True if worksheet with given name is in workbook.
- Parameters: name β Name of worksheet you are looking for
- Returns: True if the worksheet exists, False otherwise