Repository navigation
Expand file tree
/
Copy path_python_calamine.pyi
More file actions
392 lines (333 loc) · 10.9 KB
/
Copy path_python_calamine.pyi
File metadata and controls
392 lines (333 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Some documentations from upstream under MIT License. See authors in https://github.com/tafia/calamine
from __future__ import annotations
import datetime
import enum
import os
import types
import typing
_CellValue = (
int
| float
| str
| bool
| datetime.time
| datetime.date
| datetime.datetime
| datetime.timedelta
)
@typing.type_check_only
class ReadBuffer(typing.Protocol):
def seek(self, __offset: int, __whence: int = ...) -> int: ...
def read(self, __size: int = ...) -> bytes | None: ...
@typing.final
class SheetTypeEnum(enum.Enum):
WorkSheet = ...
DialogSheet = ...
MacroSheet = ...
ChartSheet = ...
Vba = ...
@typing.final
class SheetVisibleEnum(enum.Enum):
Visible = ...
"""Visible."""
Hidden = ...
"""Hidden."""
VeryHidden = ...
"""The sheet is hidden and cannot be displayed using the user interface. It is supported only by Excel formats."""
@typing.final
class SheetMetadata:
name: str
"""Name of sheet."""
typ: SheetTypeEnum
"""Type of sheet.
Only Excel formats support this. Default value for ODS is `WorkSheet`.
"""
visible: SheetVisibleEnum
"""Visible of sheet."""
def __new__(
cls, name: str, typ: SheetTypeEnum, visible: SheetVisibleEnum
) -> SheetMetadata: ...
@typing.final
class CalamineSheet:
name: str
@property
def height(self) -> int:
"""Get the row height of a sheet data.
The height is defined as the number of rows between the start and end positions.
"""
@property
def width(self) -> int:
"""Get the column width of a sheet data.
The width is defined as the number of columns between the start and end positions.
"""
@property
def total_height(self) -> int: ...
@property
def total_width(self) -> int: ...
@property
def start(self) -> tuple[int, int] | None:
"""Get top left cell position of a sheet data."""
@property
def end(self) -> tuple[int, int] | None:
"""Get bottom right cell position of a sheet data."""
@typing.overload
def to_python(
self,
skip_empty_area: bool = True,
nrows: int | None = None,
*,
empty_as_none: typing.Literal[False] = False,
) -> list[list[_CellValue]]:
"""Return sheet rows, optionally including leading empty rows and columns."""
@typing.overload
def to_python(
self,
skip_empty_area: bool = True,
nrows: int | None = None,
*,
empty_as_none: typing.Literal[True],
) -> list[list[_CellValue | None]]: ...
@typing.overload
def to_python(
self,
skip_empty_area: bool = True,
nrows: int | None = None,
*,
empty_as_none: bool,
) -> list[list[_CellValue | None]]: ...
@typing.overload
def iter_rows(
self, *, empty_as_none: typing.Literal[False] = False
) -> typing.Iterator[list[_CellValue]]:
"""Return an iterator over sheet rows."""
@typing.overload
def iter_rows(
self, *, empty_as_none: typing.Literal[True]
) -> typing.Iterator[list[_CellValue | None]]: ...
@typing.overload
def iter_rows(
self, *, empty_as_none: bool
) -> typing.Iterator[list[_CellValue | None]]: ...
@property
def merged_cell_ranges(
self,
) -> list[tuple[tuple[int, int], tuple[int, int]]] | None:
"""Return a copy of merged cell ranges.
Support only for xlsx/xls.
Returns:
list of merged cell ranges (tuple[start coordinate, end coordinate]) or None for unsupported format
"""
@typing.final
class CalamineTable:
name: str
"""Get the name of the table."""
sheet: str
"""Get the name of the parent worksheet for a table."""
columns: list[str]
"""Get the header names of the table columns.
In Excel table headers can be hidden but the table will still have
column header names.
"""
@property
def height(self) -> int:
"""Get the row height of a table data.
The height is defined as the number of rows between the start and end positions.
"""
@property
def width(self) -> int:
"""Get the column width of a table data.
The width is defined as the number of columns between the start and end positions.
"""
@property
def start(self) -> tuple[int, int] | None:
"""Get top left cell position of a table data."""
@property
def end(self) -> tuple[int, int] | None:
"""Get bottom right cell position of a table data."""
def to_python(
self,
) -> list[
list[
int
| float
| str
| bool
| datetime.time
| datetime.date
| datetime.datetime
| datetime.timedelta
]
]:
"""Returning data from table as list of lists."""
@typing.final
class WorkbookProperties:
"""Document properties of a workbook.
Fields that the file doesn't store are `None`. `created` and `modified` are
timezone-aware when the file stores a UTC offset (xlsx, xlsb and xls always do)
and naive otherwise, for example in ods files saved by LibreOffice. A date the
file stores in another format is `None`.
"""
creator: str | None
"""Creator (author) of the workbook."""
last_modified_by: str | None
"""User who last saved the workbook."""
created: datetime.datetime | None
"""Creation date."""
modified: datetime.datetime | None
"""Last modification date."""
title: str | None
"""Title."""
subject: str | None
"""Subject."""
description: str | None
"""Description or comments."""
keywords: str | None
"""Keywords."""
category: str | None
"""Category."""
content_status: str | None
"""Content status."""
revision: str | None
"""Revision number."""
version: str | None
"""Version."""
application: str | None
"""Name of the application that created the workbook."""
app_version: str | None
"""Version of the application that created the workbook."""
company: str | None
"""Company."""
template: str | None
"""Template the workbook is based on."""
manager: str | None
"""Manager."""
custom_properties: dict[str, str]
"""Custom document properties, keyed by name.
Values are returned as the text stored in the file, for example `"4"`,
`"true"` or `"2020-08-24T20:19:22Z"`.
"""
@typing.final
class CalamineWorkbook:
path: str | None
"""Path to file. `None` if bytes was loaded."""
sheet_names: list[str]
"""All sheet names of this workbook, in workbook order."""
sheets_metadata: list[SheetMetadata]
"""All sheets metadata of this workbook, in workbook order."""
table_names: list[str] | None
"""All table names of this workbook."""
@property
def workbook_properties(self) -> WorkbookProperties:
"""Document properties of this workbook, such as the creator and title.
Supported for xlsx, xlsb, xls and ods. The properties are read when this
attribute is first accessed, not when the workbook is opened.
Raises:
WorkbookClosed: If workbook already closed.
"""
@classmethod
def from_object(
cls, path_or_filelike: str | os.PathLike | ReadBuffer, load_tables: bool = False
) -> "CalamineWorkbook":
"""Determining type of pyobject and reading from it.
Args:
path_or_filelike (str | os.PathLike | ReadBuffer): path to file or IO (must implement read/seek methods).
load_tables (bool): load Excel tables (supported for XLSX only).
"""
@classmethod
def from_path(
cls, path: str | os.PathLike, load_tables: bool = False
) -> "CalamineWorkbook":
"""Reading file from path.
Args:
path (str | os.PathLike): path to file.
load_tables (bool): load Excel tables (supported for XLSX only).
"""
@classmethod
def from_filelike(
cls, filelike: ReadBuffer, load_tables: bool = False
) -> "CalamineWorkbook":
"""Reading file from IO.
Args:
filelike : IO (must implement read/seek methods).
load_tables (bool): load Excel tables (supported for XLSX only).
"""
def close(self) -> None:
"""Close the workbook.
Drop internal rust structure from workbook (and close the file under the hood).
`get_sheet_by_name`/`get_sheet_by_index` will raise WorkbookClosed after calling that method.
Raises:
WorkbookClosed: If workbook already closed.
"""
def __enter__(self) -> "CalamineWorkbook": ...
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: types.TracebackType | None,
) -> None: ...
def get_sheet_by_name(self, name: str) -> CalamineSheet:
"""Get worksheet by name.
Args:
name(str): name of worksheet
Returns:
CalamineSheet
Raises:
WorkbookClosed: If workbook already closed.
WorksheetNotFound: If worksheet not found in workbook.
"""
def get_sheet_by_index(self, index: int) -> CalamineSheet:
"""Get worksheet by index.
Args:
index(int): index of worksheet
Returns:
CalamineSheet
Raises:
WorkbookClosed: If workbook already closed.
WorksheetNotFound: If worksheet not found in workbook.
"""
def get_table_by_name(self, name: str) -> CalamineTable:
"""Get table by name.
Args:
name(str): name of table
Returns:
CalamineTable
Raises:
WorkbookClosed: If workbook already closed.
TableNotFound: If table not found in workbook.
"""
class CalamineError(Exception): ...
class PasswordError(CalamineError): ...
class WorksheetNotFound(CalamineError): ...
class XmlError(CalamineError): ...
class ZipError(CalamineError): ...
class WorkbookClosed(CalamineError): ...
class TablesNotLoaded(CalamineError): ...
class TablesNotSupported(CalamineError): ...
class TableNotFound(CalamineError): ...
def load_workbook(
path_or_filelike: str | os.PathLike | ReadBuffer, load_tables: bool = False
) -> CalamineWorkbook:
"""Determining type of pyobject and reading from it.
Args:
path_or_filelike (str | os.PathLike | ReadBuffer): path to file or IO (must implement read/seek methods).
load_tables (bool): load Excel tables (supported for XLSX only).
"""
__all__ = [
"CalamineError",
"CalamineSheet",
"CalamineTable",
"CalamineWorkbook",
"PasswordError",
"SheetMetadata",
"SheetTypeEnum",
"SheetVisibleEnum",
"TableNotFound",
"TablesNotLoaded",
"TablesNotSupported",
"WorkbookClosed",
"WorkbookProperties",
"WorksheetNotFound",
"XmlError",
"ZipError",
"load_workbook",
]