1+ # Some documentations from upstream under MIT License. See authors in https://github.com/tafia/calamine
12from __future__ import annotations
23
3- import contextlib
44import datetime
55import enum
66import os
@@ -23,34 +23,57 @@ class SheetTypeEnum(enum.Enum):
2323@typing .final
2424class SheetVisibleEnum (enum .Enum ):
2525 Visible = ...
26+ """Visible."""
2627 Hidden = ...
28+ """Hidden."""
2729 VeryHidden = ...
30+ """The sheet is hidden and cannot be displayed using the user interface. It is supported only by Excel formats."""
2831
2932@typing .final
3033class SheetMetadata :
3134 name : str
35+ """Name of sheet."""
3236 typ : SheetTypeEnum
37+ """Type of sheet.
38+
39+ Only Excel formats support this. Default value for ODS is `WorkSheet`.
40+ """
3341 visible : SheetVisibleEnum
42+ """Visible of sheet."""
3443
35- def __init__ (
36- self , name : str , typ : SheetTypeEnum , visible : SheetVisibleEnum
37- ) -> None : ...
44+ def __new__ (
45+ cls , name : str , typ : SheetTypeEnum , visible : SheetVisibleEnum
46+ ) -> SheetMetadata : ...
3847
3948@typing .final
4049class CalamineSheet :
4150 name : str
4251 @property
43- def height (self ) -> int : ...
52+ def height (self ) -> int :
53+ """Get the row height of a sheet data.
54+
55+ The height is defined as the number of rows between the start and end positions.
56+ """
57+
4458 @property
45- def width (self ) -> int : ...
59+ def width (self ) -> int :
60+ """Get the column width of a sheet data.
61+
62+ The width is defined as the number of columns between the start and end positions.
63+ """
64+
4665 @property
4766 def total_height (self ) -> int : ...
4867 @property
4968 def total_width (self ) -> int : ...
5069 @property
51- def start (self ) -> tuple [int , int ] | None : ...
70+ def start (self ) -> tuple [int , int ] | None :
71+ """Get top left cell position of a sheet data."""
72+
5273 @property
53- def end (self ) -> tuple [int , int ] | None : ...
74+ def end (self ) -> tuple [int , int ] | None :
75+ """Get bottom right cell position of a sheet data."""
76+
5477 def to_python (
5578 self , skip_empty_area : bool = True , nrows : int | None = None
5679 ) -> list [
@@ -102,34 +125,96 @@ class CalamineSheet:
102125 """
103126
104127@typing .final
105- class CalamineWorkbook (contextlib .AbstractContextManager ):
128+ class CalamineTable :
129+ name : str
130+ """Get the name of the table."""
131+ sheet : str
132+ """Get the name of the parent worksheet for a table."""
133+ columns : list [str ]
134+ """Get the header names of the table columns.
135+
136+ In Excel table headers can be hidden but the table will still have
137+ column header names.
138+ """
139+ @property
140+ def height (self ) -> int :
141+ """Get the row height of a table data.
142+
143+ The height is defined as the number of rows between the start and end positions.
144+ """
145+
146+ @property
147+ def width (self ) -> int :
148+ """Get the column width of a table data.
149+
150+ The width is defined as the number of columns between the start and end positions.
151+ """
152+
153+ @property
154+ def start (self ) -> tuple [int , int ] | None :
155+ """Get top left cell position of a table data."""
156+
157+ @property
158+ def end (self ) -> tuple [int , int ] | None :
159+ """Get bottom right cell position of a table data."""
160+
161+ def to_python (
162+ self ,
163+ ) -> list [
164+ list [
165+ int
166+ | float
167+ | str
168+ | bool
169+ | datetime .time
170+ | datetime .date
171+ | datetime .datetime
172+ | datetime .timedelta
173+ ]
174+ ]:
175+ """Retunrning data from table as list of lists."""
176+
177+ @typing .final
178+ class CalamineWorkbook :
106179 path : str | None
180+ """Path to file. `None` if bytes was loaded."""
107181 sheet_names : list [str ]
182+ """All sheet names of this workbook, in workbook order."""
108183 sheets_metadata : list [SheetMetadata ]
184+ """All sheets metadata of this workbook, in workbook order."""
185+ table_names : list [str ] | None
186+ """All table names of this workbook."""
109187 @classmethod
110188 def from_object (
111- cls , path_or_filelike : str | os .PathLike | ReadBuffer
189+ cls , path_or_filelike : str | os .PathLike | ReadBuffer , load_tables : bool = False
112190 ) -> "CalamineWorkbook" :
113191 """Determining type of pyobject and reading from it.
114192
115193 Args:
116194 path_or_filelike (str | os.PathLike | ReadBuffer): path to file or IO (must imlpement read/seek methods).
195+ load_tables (bool): load Excel tables (supported for XLSX only).
117196 """
118197
119198 @classmethod
120- def from_path (cls , path : str | os .PathLike ) -> "CalamineWorkbook" :
199+ def from_path (
200+ cls , path : str | os .PathLike , load_tables : bool = False
201+ ) -> "CalamineWorkbook" :
121202 """Reading file from path.
122203
123204 Args:
124205 path (str | os.PathLike): path to file.
206+ load_tables (bool): load Excel tables (supported for XLSX only).
125207 """
126208
127209 @classmethod
128- def from_filelike (cls , filelike : ReadBuffer ) -> "CalamineWorkbook" :
210+ def from_filelike (
211+ cls , filelike : ReadBuffer , load_tables : bool = False
212+ ) -> "CalamineWorkbook" :
129213 """Reading file from IO.
130214
131215 Args:
132216 filelike : IO (must imlpement read/seek methods).
217+ load_tables (bool): load Excel tables (supported for XLSX only).
133218 """
134219
135220 def close (self ) -> None :
@@ -177,18 +262,55 @@ class CalamineWorkbook(contextlib.AbstractContextManager):
177262 WorksheetNotFound: If worksheet not found in workbook.
178263 """
179264
265+ def get_table_by_name (self , name : str ) -> CalamineTable :
266+ """Get table by name.
267+
268+ Args:
269+ name(str): name of table
270+
271+ Returns:
272+ CalamineTable
273+
274+ Raises:
275+ WorkbookClosed: If workbook already closed.
276+ WorksheetNotFound: If worksheet not found in workbook.
277+ """
278+
180279class CalamineError (Exception ): ...
181280class PasswordError (CalamineError ): ...
182281class WorksheetNotFound (CalamineError ): ...
183282class XmlError (CalamineError ): ...
184283class ZipError (CalamineError ): ...
185284class WorkbookClosed (CalamineError ): ...
285+ class TablesNotLoaded (CalamineError ): ...
286+ class TablesNotSupported (CalamineError ): ...
287+ class TableNotFound (CalamineError ): ...
186288
187289def load_workbook (
188- path_or_filelike : str | os .PathLike | ReadBuffer ,
290+ path_or_filelike : str | os .PathLike | ReadBuffer , load_tables : bool = False
189291) -> CalamineWorkbook :
190292 """Determining type of pyobject and reading from it.
191293
192294 Args:
193295 path_or_filelike (str | os.PathLike | ReadBuffer): path to file or IO (must imlpement read/seek methods).
296+ load_tables (bool): load Excel tables (supported for XLSX only).
194297 """
298+
299+ __all__ = [
300+ "CalamineError" ,
301+ "CalamineSheet" ,
302+ "CalamineTable" ,
303+ "CalamineWorkbook" ,
304+ "PasswordError" ,
305+ "SheetMetadata" ,
306+ "SheetTypeEnum" ,
307+ "SheetVisibleEnum" ,
308+ "TableNotFound" ,
309+ "TablesNotLoaded" ,
310+ "TablesNotSupported" ,
311+ "WorkbookClosed" ,
312+ "WorksheetNotFound" ,
313+ "XmlError" ,
314+ "ZipError" ,
315+ "load_workbook" ,
316+ ]
0 commit comments