msl.io.readers.spreadsheet module

Generic class for spreadsheets.

class msl.io.readers.spreadsheet.Spreadsheet(file)[source]

Bases: object

Generic class for spreadsheets.

Parameters:

file (str) – The location of the spreadsheet on a local hard drive or on a network.

property file

The location of the spreadsheet on a local hard drive or on a network.

Type:

str

read(cell=None, sheet=None, as_datetime=True)[source]

Read values from the spreadsheet.

Parameters:
  • cell (str, optional) – The cell(s) to read. For example, C9 will return a single value and C9:G20 will return all values in the specified range. If not specified then returns all values in the specified sheet.

  • sheet (str, optional) – The name of the sheet to read the value(s) from. If there is only one sheet in the spreadsheet then you do not need to specify the name of the sheet.

  • as_datetime (bool, optional) – Whether dates should be returned as datetime or date objects. If False then dates are returned as a string.

Returns:

The value(s) of the requested cell(s).

sheet_names()[source]

Get the names of all sheets in the spreadsheet.

Returns:

tuple of str – The names of all sheets.

static to_indices(cell)[source]

Convert a string representation of a cell to row and column indices.

Parameters:

cell (str) – The cell. Can be letters only (a column) or letters and a number (a column and a row).

Returns:

tuple – The (row_index, column_index). If cell does not contain a row number then the row index is None. The row and column index are zero based.

Examples

>>> to_indices('A')
(None, 0)
>>> to_indices('A1')
(0, 0)
>>> to_indices('AA10')
(9, 26)
>>> to_indices('AAA111')
(110, 702)
>>> to_indices('MSL123456')
(123455, 9293)
>>> to_indices('BIPM')
(None, 41664)
static to_letters(index)[source]

Convert a column index to column letters.

Parameters:

index (int) – The column index (zero based).

Returns:

str – The corresponding spreadsheet column letter(s).

Examples

>>> to_letters(0)
'A'
>>> to_letters(1)
'B'
>>> to_letters(26)
'AA'
>>> to_letters(702)
'AAA'
>>> to_letters(494264)
'ABCDE'
static to_slices(cells, row_step=None, column_step=None)[source]

Convert a range of cells to slices of row and column indices.

Parameters:
  • cells (str) – The cells. Can be letters only (a column) or letters and a number (a column and a row).

  • row_step (int, optional) – The step-by value for the row slice.

  • column_step (int, optional) – The step-by value for the column slice.

Returns:

  • slice – The row slice.

  • slice – The column slice.

Examples

>>> to_slices('A:A')
(slice(0, None, None), slice(0, 1, None))
>>> to_slices('A:H')
(slice(0, None, None), slice(0, 8, None))
>>> to_slices('B2:M10')
(slice(1, 10, None), slice(1, 13, None))
>>> to_slices('A5:M100', row_step=2, column_step=4)
(slice(4, 100, 2), slice(0, 13, 4))