spreadsheets¤
Classes for reading cells in spreadsheets.
Spreadsheet ¤
Spreadsheet(file)
Bases: ABC
Abstract base class for spreadsheets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The location of the spreadsheet on a local hard drive or on a network drive. |
required |
Source code in src/msl/io/readers/spreadsheet.py
19 20 21 22 23 24 25 |
|
file
property
¤
file
str — The location of the spreadsheet on a local hard drive or on a network drive.
read
abstractmethod
¤
read(
cells=None,
sheet=None,
*,
as_datetime=True,
merged=False,
)
Read values from the spreadsheet.
You must override this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
str | None
|
The cell(s) to read. For example, |
None
|
sheet
|
str | None
|
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. |
None
|
as_datetime
|
bool
|
True
|
|
merged
|
bool
|
Applies to cells that are merged with other cells. The details depend on the type of spreadsheet document that is being read. Some documents allow the hidden cells that are part of a merger to retain its unmerged value. Other documents associate the merged value only with the top-left cell and all other cells in the merger are empty. |
False
|
Returns:
Type | Description |
---|---|
Any | list[tuple[Any, ...]]
|
The value(s) of the requested cell(s). |
Source code in src/msl/io/readers/spreadsheet.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
sheet_names
abstractmethod
¤
sheet_names()
Get the names of all sheets in the spreadsheet.
You must override this method.
Returns:
Type | Description |
---|---|
tuple[str, ...]
|
The names of all sheets. |
Source code in src/msl/io/readers/spreadsheet.py
59 60 61 62 63 64 65 66 67 |
|
to_indices
staticmethod
¤
to_indices(cell)
Convert a string representation of a cell to row and column indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cell
|
str
|
The cell. Can be letters only (a column) or letters and a number (a column and a row). |
required |
Returns:
Type | Description |
---|---|
tuple[int | None, int]
|
The (row_index, column_index). If |
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)
Source code in src/msl/io/readers/spreadsheet.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
to_letters
staticmethod
¤
to_letters(index)
Convert a column index to column letters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
The column index (zero based). |
required |
Returns:
Type | Description |
---|---|
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'
Source code in src/msl/io/readers/spreadsheet.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
to_slices
staticmethod
¤
to_slices(cells, row_step=None, column_step=None)
Convert a range of cells to slices of row and column indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
str
|
The cells. Can be letters only (a column) or letters and a number (a column and a row). |
required |
row_step
|
int | None
|
The step-by value for the row slice. |
None
|
column_step
|
int | None
|
The step-by value for the column slice. |
None
|
Returns:
Type | Description |
---|---|
tuple[slice[int, int | None, int | None], slice[int, int, int | None]]
|
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))
Source code in src/msl/io/readers/spreadsheet.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
Read an Excel spreadsheet (.xls and .xlsx files).
ExcelReader ¤
ExcelReader(file, **kwargs)
Bases: Spreadsheet
Read an Excel spreadsheet (.xls and .xlsx files).
This class simply provides a convenience for reading cell values (not drawings or charts) from Excel spreadsheets. It is not registered as a Reader because the information in a spreadsheet is unstructured and therefore one cannot generalize how to parse a spreadsheet to create a Root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike
|
The path to an Excel spreadsheet file. |
required |
kwargs
|
Any
|
All keyword arguments are passed to xlrd.open_workbook.
You can use an |
{}
|
Examples:
from msl.io import ExcelReader
excel = ExcelReader("lab_environment.xlsx")
Source code in src/msl/io/readers/excel.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
close ¤
close()
Close the workbook.
Source code in src/msl/io/readers/excel.py
86 87 88 |
|
dimensions ¤
dimensions(sheet)
Get the number of rows and columns in a sheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
str
|
The name of a sheet to get the dimensions of. |
required |
Returns:
Type | Description |
---|---|
tuple[int, int]
|
The (number of rows, number of columns) in |
Source code in src/msl/io/readers/excel.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
read ¤
read(
cells=None,
sheet=None,
*,
as_datetime=True,
merged=False,
)
Read cell values from the Excel spreadsheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
str | None
|
The cell(s) to read. For example, |
None
|
sheet
|
str | None
|
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. |
None
|
as_datetime
|
bool
|
True
|
|
merged
|
bool
|
Applies to cells that are merged with other cells. If cells are merged, then
only the top-left cell has the value and all other cells in the merger are empty.
Enabling this argument is currently not supported and the value must be |
False
|
Returns:
Type | Description |
---|---|
Any | list[tuple[Any, ...]]
|
The value(s) of the requested cell(s). |
Examples:
>>> excel.read()
[('temperature', 'humidity'), (20.33, 49.82), (20.23, 46.06), (20.41, 47.06), (20.29, 48.32)]
>>> excel.read("B2")
49.82
>>> excel.read("A:A")
[('temperature',), (20.33,), (20.23,), (20.41,), (20.29,)]
>>> excel.read("A1:B1")
[('temperature', 'humidity')]
>>> excel.read("A2:B4")
[(20.33, 49.82), (20.23, 46.06), (20.41, 47.06)]
Source code in src/msl/io/readers/excel.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
sheet_names ¤
sheet_names()
Get the names of all sheets in the Excel spreadsheet.
Returns:
Type | Description |
---|---|
tuple[str, ...]
|
The names of all sheets. |
Source code in src/msl/io/readers/excel.py
201 202 203 204 205 206 207 |
|
Read a Google Sheets spreadsheet.
GSheetsReader ¤
GSheetsReader(file, *, account=None, credentials=None)
Bases: Spreadsheet
Read a Google Sheets spreadsheet.
This class simply provides a convenience for reading cell values (not drawings or charts) from Google spreadsheets. It is not registered as a Reader because the information in a spreadsheet is unstructured and therefore one cannot generalize how to parse a spreadsheet to create a Root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike
|
The ID or path of a Google Sheets spreadsheet. |
required |
account
|
str | None
|
Since a person may have multiple Google accounts, and multiple people
may run the same code, this parameter decides which token to load
to authenticate with the Google API. The value can be any text (or
|
None
|
credentials
|
PathLike | None
|
The path to the client secrets OAuth credential file. This parameter only needs to be specified the first time that you authenticate with a particular Google account or if you delete the token file that was created when you previously authenticated. |
None
|
Examples:
from msl.io import GSheetsReader
# Specify the path
sheets = GSheetsReader("Google Drive/registers/equipment.gsheet")
# Specify the ID
sheets = GSheetsReader("1TI3pM-534SZ5DQTEZ-7HCI04648f8ZpLGbfHWJu9FSo")
Source code in src/msl/io/readers/gsheets.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
close ¤
close()
Close the connection to the GSheet API service.
Source code in src/msl/io/readers/gsheets.py
104 105 106 |
|
read ¤
read(
cells=None,
sheet=None,
*,
as_datetime=True,
merged=False,
)
Read cell values from the Google Sheets spreadsheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
str | None
|
The cell(s) to read. For example, |
None
|
sheet
|
str | None
|
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. |
None
|
as_datetime
|
bool
|
True
|
|
merged
|
bool
|
Applies to cells that are merged with other cells. If cells are merged, then
only the top-left cell has the value and all other cells in the merger are empty.
Enabling this argument is currently not supported and the value must be |
False
|
Returns:
Type | Description |
---|---|
Any | list[tuple[Any, ...]]
|
The value(s) of the requested cell(s). |
Examples:
>>> sheets.read()
[('temperature', 'humidity'), (20.33, 49.82), (20.23, 46.06), (20.41, 47.06), (20.29, 48.32)]
>>> sheets.read("B2")
49.82
>>> sheets.read("A:A")
[('temperature',), (20.33,), (20.23,), (20.41,), (20.29,)]
>>> sheets.read("A1:B1")
[('temperature', 'humidity')]
>>> sheets.read("A2:B4")
[(20.33, 49.82), (20.23, 46.06), (20.41, 47.06)]
Source code in src/msl/io/readers/gsheets.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
sheet_names ¤
sheet_names()
Get the names of all sheets in the Google Sheets spreadsheet.
Returns:
Type | Description |
---|---|
tuple[str, ...]
|
The names of all sheets. |
Source code in src/msl/io/readers/gsheets.py
201 202 203 204 205 206 207 |
|
Read an OpenDocument Spreadsheet (.ods and .fods files, Version 1.2).
ODSReader ¤
ODSReader(file, **kwargs)
Bases: Spreadsheet
Read an OpenDocument Spreadsheet (.ods and .fods files, Version 1.2).
This class simply provides a convenience for reading cell values (not drawings or charts) from OpenDocument Spreadsheets. It is not registered as a Reader because the information in a spreadsheet is unstructured and therefore one cannot generalize how to parse a spreadsheet to create a Root.
Tip
If defusedxml is installed, that package is used to parse the contents of the file instead of the xml module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike
|
The path to an OpenDocument Spreadsheet file. |
required |
kwargs
|
Any
|
All keyword arguments are ignored. |
{}
|
Examples:
from msl.io import ODSReader
ods = ODSReader("lab_environment.ods")
Source code in src/msl/io/readers/ods.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
close ¤
close()
Free memory resources that are used to read the OpenDocument Spreadsheet.
Source code in src/msl/io/readers/ods.py
102 103 104 105 |
|
dimensions ¤
dimensions(sheet)
Get the number of rows and columns in a sheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
str
|
The name of a sheet to get the dimensions of. |
required |
Returns:
Type | Description |
---|---|
tuple[int, int]
|
The (number of rows, number of columns) in |
Source code in src/msl/io/readers/ods.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
read ¤
read(
cells=None,
sheet=None,
*,
as_datetime=True,
merged=False,
)
Read cell values from the OpenDocument Spreadsheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
str | None
|
The cell(s) to read. For example, |
None
|
sheet
|
str | None
|
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. |
None
|
as_datetime
|
bool
|
True
|
|
merged
|
bool
|
Applies to cells that are merged with other cells. If |
False
|
Returns:
Type | Description |
---|---|
Any | list[tuple[Any, ...]]
|
The value(s) of the requested cell(s). |
Examples:
>>> ods.read()
[('temperature', 'humidity'), (20.33, 49.82), (20.23, 46.06), (20.41, 47.06), (20.29, 48.32)]
>>> ods.read("B2")
49.82
>>> ods.read("A:A")
[('temperature',), (20.33,), (20.23,), (20.41,), (20.29,)]
>>> ods.read("A1:B1")
[('temperature', 'humidity')]
>>> ods.read("A2:B4")
[(20.33, 49.82), (20.23, 46.06), (20.41, 47.06)]
Source code in src/msl/io/readers/ods.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
sheet_names ¤
sheet_names()
Get the names of all sheets in the OpenDocument Spreadsheet.
Returns:
Type | Description |
---|---|
tuple[str, ...]
|
The names of all sheets. |
Source code in src/msl/io/readers/ods.py
224 225 226 227 228 229 230 |
|