Skip to content

Commit ac0f272

Browse files
committed
unify the read and write interface among pyexcel plugins
1 parent 44b3bdc commit ac0f272

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

README.rst

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,48 +61,40 @@ Write to an xls file
6161
... from StringIO import StringIO
6262
... else:
6363
... from io import BytesIO as StringIO
64-
>>> from pyexcel.ext.xls import OrderedDict
64+
>>> from pyexcel_io import OrderedDict
6565

6666

6767
Here's the sample code to write a dictionary to an xls file::
6868

69-
>>> from pyexcel_xls import XLWriter
69+
>>> from pyexcel_xls import store_data
7070
>>> data = OrderedDict() # from collections import OrderedDict
7171
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
7272
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
73-
>>> writer = XLWriter("your_file.xls")
74-
>>> writer.write(data)
75-
>>> writer.close()
73+
>>> store_data("your_file.xls", data)
7674

7775
Read from an xls file
7876
**********************
7977

8078
Here's the sample code::
8179

82-
>>> from pyexcel_xls import XLBook
83-
84-
>>> book = XLBook("your_file.xls")
85-
>>> # book.sheets() returns a dictionary of all sheet content
86-
>>> # the keys represents sheet names
87-
>>> # the values are two dimensional array
88-
>>> import json
89-
>>> print(json.dumps(book.sheets()))
80+
>>> from pyexcel_xls import load_data
81+
>>> data = load_data("your_file.xls")
82+
>>> import json
83+
>>> print(json.dumps(data))
9084
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
9185

9286
Write an xls to memory
9387
**********************
9488

9589
Here's the sample code to write a dictionary to an xls file::
9690

97-
>>> from pyexcel_xls import XLWriter
91+
>>> from pyexcel_xls import store_data
9892
>>> data = OrderedDict()
9993
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
10094
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
10195
>>> io = StringIO()
102-
>>> writer = XLWriter(io)
103-
>>> writer.write(data)
104-
>>> writer.close()
105-
>>> # do something witht the io
96+
>>> writer = store_data(io, data)
97+
>>> # do something with the io
10698
>>> # In reality, you might give it to your http response
10799
>>> # object for downloading
108100

@@ -115,8 +107,8 @@ Continue from previous example::
115107
>>> # This is just an illustration
116108
>>> # In reality, you might deal with xls file upload
117109
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
118-
>>> book = XLBook(None, io.getvalue())
119-
>>> print(json.dumps(book.sheets()))
110+
>>> data = load_data(io)
111+
>>> print(json.dumps(data))
120112
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]}
121113

122114

@@ -169,7 +161,7 @@ You got to wrap the binary content with stream to get xls working::
169161
>>> xlfile = "another_file.xls"
170162
>>> with open(xlfile, "rb") as f:
171163
... content = f.read()
172-
... r = pe.get_book(file_type="xls", content=content)
164+
... r = pe.get_book(file_type="xls", file_content=content)
173165
... print(r)
174166
...
175167
Sheet Name: Sheet 1

pyexcel_xls/__init__.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
SheetWriter,
1818
BookWriter,
1919
READERS,
20-
WRITERS
20+
WRITERS,
21+
isstream,
22+
load_data as read_data,
23+
store_data as write_data
2124
)
22-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
23-
from ordereddict import OrderedDict
24-
else:
25-
from collections import OrderedDict
25+
PY2 = sys.version_info[0] == 2
2626

2727

2828
DEFAULT_DATE_FORMAT = "DD/MM/YY"
@@ -137,7 +137,7 @@ def load_from_memory(self, file_content, **keywords):
137137
on_demand = False
138138
if self.sheet_name is not None or self.sheet_index is not None:
139139
on_demand = True
140-
return xlrd.open_workbook(None, file_contents=file_content,
140+
return xlrd.open_workbook(None, file_contents=file_content.getvalue(),
141141
on_demand=on_demand)
142142

143143
def load_from_file(self, filename, **keywords):
@@ -221,4 +221,28 @@ def close(self):
221221
})
222222

223223

224+
def is_string(atype):
225+
"""find out if a type is str or not"""
226+
if atype == str:
227+
return True
228+
elif PY2:
229+
if atype == unicode:
230+
return True
231+
elif atype == str:
232+
return True
233+
return False
234+
235+
236+
def store_data(afile, data, file_type=None, **keywords):
237+
if isstream(afile) and file_type is None:
238+
file_type='xls'
239+
write_data(afile, data, file_type=file_type, **keywords)
240+
241+
242+
def load_data(afile, file_type=None, **keywords):
243+
if isstream(afile) and file_type is None:
244+
file_type='xls'
245+
return read_data(afile, file_type=file_type, **keywords)
246+
247+
224248
__VERSION__ = "0.0.7"

0 commit comments

Comments
 (0)