44
55 ods reader
66
7- :copyright: (c) 2015-2017 by Onni Software Ltd. & its contributors
7+ :copyright: (c) 2015-2020 by Onni Software Ltd. & its contributors
88 :license: New BSD License
99"""
10- import ezodf
10+ from io import BytesIO
1111
12+ import ezodf
1213import pyexcel_io .service as service
13- from pyexcel_io .book import BookReader
14- from pyexcel_io .sheet import SheetReader
15- from pyexcel_io ._compact import OrderedDict
14+ from pyexcel_io .plugin_api import ISheet , IReader , NamedContent
1615
1716
18- class ODSSheet (SheetReader ):
17+ class ODSSheet (ISheet ):
1918 """ODS sheet representation"""
2019
21- def __init__ (self , sheet , auto_detect_int = True , ** keywords ):
22- SheetReader .__init__ (self , sheet , ** keywords )
20+ def __init__ (self , sheet , auto_detect_int = True ):
2321 self .auto_detect_int = auto_detect_int
22+ self .ods_sheet = sheet
2423
25- @property
26- def name (self ):
27- return self ._native_sheet .name
28-
29- def number_of_rows (self ):
24+ def row_iterator (self ):
3025 """
3126 Number of rows in the xls sheet
3227 """
33- return self ._native_sheet .nrows ()
28+ return range ( self .ods_sheet .nrows () )
3429
35- def number_of_columns (self ):
30+ def column_iterator (self , row ):
3631 """
3732 Number of columns in the xls sheet
3833 """
39- return self ._native_sheet .ncols ()
34+ for column in range (self .ods_sheet .ncols ()):
35+ yield self .cell_value (row , column )
4036
4137 def cell_value (self , row , column ):
42- cell = self ._native_sheet .get_cell ((row , column ))
38+ cell = self .ods_sheet .get_cell ((row , column ))
4339 cell_type = cell .value_type
4440 ret = None
4541 if cell_type == "currency" :
@@ -63,63 +59,28 @@ def cell_value(self, row, column):
6359 return ret
6460
6561
66- class ODSBook (BookReader ):
67- """read a ods book out"""
68-
69- def open (self , file_name , ** keywords ):
70- """load ods from file"""
71- BookReader .open (self , file_name , ** keywords )
72- self ._load_from_file ()
73-
74- def open_stream (self , file_stream , ** keywords ):
75- """load ods from file stream"""
76- BookReader .open_stream (self , file_stream , ** keywords )
77- self ._load_from_memory ()
78-
79- def read_sheet_by_name (self , sheet_name ):
80- """read a named sheet"""
81- rets = [
82- sheet
83- for sheet in self ._native_book .sheets
84- if sheet .name == sheet_name
62+ class ODSBook (IReader ):
63+ def __init__ (self , file_alike_object , file_type , ** keywords ):
64+ self .ods_book = ezodf .opendoc (file_alike_object )
65+ self ._keywords = keywords
66+ self .content_array = [
67+ NamedContent (sheet .name , sheet ) for sheet in self .ods_book .sheets
8568 ]
86- if len (rets ) == 0 :
87- raise ValueError ("%s cannot be found" % sheet_name )
88- elif len (rets ) == 1 :
89- return self .read_sheet (rets [0 ])
90- else :
91- raise ValueError (
92- "More than 1 sheet named as %s are found" % sheet_name
93- )
94-
95- def read_sheet_by_index (self , sheet_index ):
96- """read a sheet at an index"""
97- sheets = self ._native_book .sheets
98- length = len (sheets )
99- if sheet_index < length :
100- return self .read_sheet (sheets [sheet_index ])
101- else :
102- raise IndexError (
103- "Index %d of out bound %d." % (sheet_index , length )
104- )
105-
106- def read_all (self ):
107- """read all available sheets"""
108- result = OrderedDict ()
109- for sheet in self ._native_book .sheets :
110- data_dict = self .read_sheet (sheet )
111- result .update (data_dict )
112- return result
113-
114- def read_sheet (self , native_sheet ):
69+
70+ def read_sheet (self , native_sheet_index ):
71+ native_sheet = self .content_array [native_sheet_index ].payload
11572 sheet = ODSSheet (native_sheet , ** self ._keywords )
116- return { native_sheet . name : sheet . to_array ()}
73+ return sheet
11774
11875 def close (self ):
119- self ._native_book = None
76+ self .ods_book = None
77+
12078
121- def _load_from_file (self ):
122- self ._native_book = ezodf .opendoc (self ._file_name )
79+ class ODSBookInContent (ODSBook ):
80+ """
81+ Open xlsx as read only mode
82+ """
12383
124- def _load_from_memory (self ):
125- self ._native_book = ezodf .opendoc (self ._file_stream )
84+ def __init__ (self , file_content , file_type , ** keywords ):
85+ io = BytesIO (file_content )
86+ super ().__init__ (io , file_type , ** keywords )
0 commit comments