1
1
from datetime import date , datetime , time
2
- from io import UnsupportedOperation
2
+ from io import BytesIO
3
3
4
4
import pyexcel_io .service as service
5
- from pyexcel_io ._compact import BytesIO , OrderedDict
6
- from pyexcel_io .book import BookReader
7
- from pyexcel_io .sheet import SheetReader
5
+ from pyexcel_io .plugin_api .abstract_reader import IReader
6
+ from pyexcel_io .plugin_api .abstract_sheet import ISheet
8
7
9
8
from pyexcel_xlsxr .messy_xlsx import XLSXBookSet
10
9
11
10
12
- class XLSXSheet (SheetReader ):
11
+ class XLSXSheet (ISheet ):
13
12
def __init__ (
14
13
self ,
15
14
sheet ,
@@ -18,10 +17,11 @@ def __init__(
18
17
auto_detect_datetime = True ,
19
18
** keywords
20
19
):
21
- SheetReader . __init__ ( self , sheet , ** keywords )
20
+ self . _native_sheet = sheet
22
21
self .__auto_detect_int = auto_detect_int
23
22
self .__auto_detect_float = auto_detect_float
24
23
self .__auto_detect_datetime = auto_detect_datetime
24
+ self ._keywords = keywords
25
25
26
26
@property
27
27
def name (self ):
@@ -55,60 +55,32 @@ def __convert_cell(self, cell):
55
55
return ret
56
56
57
57
58
- class XLSXBook (BookReader ):
59
- def open (self , file_name , ** keywords ):
60
- BookReader .open (self , file_name , ** keywords )
61
- self ._load_from_file ()
62
-
63
- def open_stream (self , file_stream , ** keywords ):
64
- if not hasattr (file_stream , "seek" ):
65
- # python 2
66
- # Hei zipfile in odfpy would do a seek
67
- # but stream from urlib cannot do seek
68
- file_stream = BytesIO (file_stream .read ())
69
- try :
70
- file_stream .seek (0 )
71
- except UnsupportedOperation :
72
- # python 3
73
- file_stream = BytesIO (file_stream .read ())
74
- BookReader .open_stream (self , file_stream , ** keywords )
75
- self ._load_from_memory ()
76
-
77
- def read_sheet_by_name (self , sheet_name ):
58
+ class XLSXBook (IReader ):
59
+ def __init__ (self , file_alike_object , _ , ** keywords ):
60
+ self ._native_book = XLSXBookSet (file_alike_object )
61
+ self ._keywords = keywords
78
62
tables = self ._native_book .make_tables ()
79
- rets = [table for table in tables if table .name == sheet_name ]
80
- if len (rets ) == 0 :
81
- raise ValueError ("%s cannot be found" % sheet_name )
82
- else :
83
- return self .read_sheet (rets [0 ])
63
+ self .content_array = [
64
+ NameObject (table .name , table ) for table in tables
65
+ ]
84
66
85
- def read_sheet_by_index (self , sheet_index ):
67
+ def read_sheet (self , sheet_index ):
86
68
"""read a sheet at a specified index"""
87
- tables = self ._native_book .make_tables ()
88
- length = len (tables )
89
- if sheet_index < length :
90
- return self .read_sheet (tables [sheet_index ])
91
- else :
92
- raise IndexError (
93
- "Index %d of out bound %d" % (sheet_index , length )
94
- )
95
-
96
- def read_all (self ):
97
- """read all sheets"""
98
- result = OrderedDict ()
99
- for sheet in self ._native_book .make_tables ():
100
- ods_sheet = XLSXSheet (sheet , ** self ._keywords )
101
- result [ods_sheet .name ] = ods_sheet .to_array ()
102
-
103
- return result
104
-
105
- def read_sheet (self , native_sheet ):
106
- """read one native sheet"""
107
- sheet = XLSXSheet (native_sheet , ** self ._keywords )
108
- return {sheet .name : sheet .to_array ()}
109
-
110
- def _load_from_memory (self ):
111
- self ._native_book = XLSXBookSet (self ._file_stream )
112
-
113
- def _load_from_file (self ):
114
- self ._native_book = XLSXBookSet (self ._file_name )
69
+ table = self .content_array [sheet_index ].sheet
70
+ sheet = XLSXSheet (table , ** self ._keywords )
71
+ return sheet
72
+
73
+ def close (self ):
74
+ self ._native_book .close ()
75
+
76
+
77
+ class XLSXBookInContent (XLSXBook ):
78
+ def __init__ (self , file_content , file_type , ** keywords ):
79
+ file_stream = BytesIO (file_content )
80
+ super ().__init__ (file_stream , file_type , ** keywords )
81
+
82
+
83
+ class NameObject (object ):
84
+ def __init__ (self , name , sheet ):
85
+ self .name = name
86
+ self .sheet = sheet
0 commit comments