Skip to content

Commit 521def6

Browse files
committed
unify the read and write interface among pyexcel plugins
1 parent cfbe5d3 commit 521def6

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

README.rst

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ Installation
3737

3838
You can install it via pip::
3939

40-
$ pip install git+https://github.com/chfw/ezodf.git
40+
$ pip install git+https://github.com/T0ha/ezodf.git
4141
$ pip install pyexcel-ods3
4242

4343
or clone it and install it::
4444

45-
$ pip install git+https://github.com/chfw/ezodf.git
45+
$ pip install git+https://github.com/T0ha/ezodf.git
4646
$ pip install git+https://github.com/chfw/pyexcel-ods3.git
4747
$ cd pyexcel-ods3
4848
$ python setup.py install
@@ -64,50 +64,43 @@ As a standalone library
6464
... from StringIO import StringIO
6565
... else:
6666
... from io import BytesIO as StringIO
67-
>>> from pyexcel_xls import OrderedDict
67+
>>> from pyexcel_io import OrderedDict
6868

6969

7070
Write to an ods file
7171
*********************
7272

7373
Here's the sample code to write a dictionary to an ods file::
7474

75-
>>> from pyexcel_ods3 import ODSWriter
75+
>>> from pyexcel_ods3 import store_data
7676
>>> data = OrderedDict()
7777
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
7878
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
79-
>>> writer = ODSWriter("your_file.ods")
80-
>>> writer.write(data)
81-
>>> writer.close()
79+
>>> store_data("your_file.ods", data)
8280

8381
Read from an ods file
8482
**********************
8583

8684
Here's the sample code::
8785

88-
>>> from pyexcel_ods3 import ODSBook
89-
>>> book = ODSBook("your_file.ods")
90-
>>> # book.sheets() returns a dictionary of all sheet content
91-
>>> # the keys represents sheet names
92-
>>> # the values are two dimensional array
86+
>>> from pyexcel_ods3 import load_data
87+
>>> data = load_data("your_file.ods")
9388
>>> import json
94-
>>> print(json.dumps(book.sheets()))
89+
>>> print(json.dumps(data))
9590
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
9691

9792
Write an ods file to memory
98-
*****************************
93+
******************************
9994

10095
Here's the sample code to write a dictionary to an ods file::
10196

102-
>>> from pyexcel_ods3 import ODSWriter
97+
>>> from pyexcel_ods3 import store_data
10398
>>> data = OrderedDict()
10499
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
105100
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
106101
>>> io = StringIO()
107-
>>> writer = ODSWriter(io)
108-
>>> writer.write(data)
109-
>>> writer.close()
110-
>>> # do something witht the io
102+
>>> store_data(io, data)
103+
>>> # do something with the io
111104
>>> # In reality, you might give it to your http response
112105
>>> # object for downloading
113106

@@ -118,10 +111,11 @@ Read from an ods from memory
118111
Here's the sample code::
119112

120113
>>> # This is just an illustration
121-
>>> # In reality, you might deal with xl file upload
122-
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
123-
>>> book = ODSBook(None, io.getvalue())
124-
>>> print(json.dumps(book.sheets()))
114+
>>> # In reality, you might deal with ods file upload
115+
>>> # where you will read from requests.FILES['YOUR_ODS_FILE']
116+
>>> io.seek(0)
117+
>>> data = load_data(io)
118+
>>> print(json.dumps(data))
125119
{"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]]}
126120

127121

@@ -161,19 +155,19 @@ Here is the sample code::
161155

162156
>>> sheet.save_as("another_file.ods")
163157

164-
Reading from a StringIO instance
158+
Reading from a IO instance
165159
================================
166160

167161
You got to wrap the binary content with StringIO to get odf working::
168162

169163

170164
>>> # This is just an illustration
171165
>>> # In reality, you might deal with xl file upload
172-
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
173-
>>> xlfile = "another_file.ods"
174-
>>> with open(xlfile, "rb") as f:
166+
>>> # where you will read from requests.FILES['YOUR_ODS_FILE']
167+
>>> odsfile = "another_file.ods"
168+
>>> with open(odsfile, "rb") as f:
175169
... content = f.read()
176-
... r = pe.get_book(file_type="ods", content=content)
170+
... r = pe.get_book(file_type="ods", file_content=content)
177171
... print(r)
178172
...
179173
Sheet Name: Sheet 1

pyexcel_ods3/__init__.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
SheetWriter,
1717
BookWriter,
1818
READERS,
19-
WRITERS
19+
WRITERS,
20+
isstream,
21+
load_data as read_data,
22+
store_data as write_data
2023
)
21-
if sys.version_info[0] < 3:
22-
from StringIO import StringIO
23-
else:
24-
from io import BytesIO as StringIO
24+
import sys
25+
PY2 = sys.version_info[0] == 2
2526

2627

2728
def float_value(value):
@@ -136,7 +137,7 @@ def load_from_file(self, filename, **keywords):
136137
return ezodf.opendoc(filename)
137138

138139
def load_from_memory(self, file_content, **keywords):
139-
return ezodf.opendoc(StringIO(file_content))
140+
return ezodf.opendoc(file_content)
140141

141142
def sheet_iterator(self):
142143
if self.sheet_name is not None:
@@ -227,4 +228,29 @@ def close(self):
227228
WRITERS["ods"] = ODSWriter
228229

229230

231+
232+
def is_string(atype):
233+
"""find out if a type is str or not"""
234+
if atype == str:
235+
return True
236+
elif PY2:
237+
if atype == unicode:
238+
return True
239+
elif atype == str:
240+
return True
241+
return False
242+
243+
244+
def store_data(afile, data, file_type=None, **keywords):
245+
if isstream(afile) and file_type is None:
246+
file_type='ods'
247+
write_data(afile, data, file_type=file_type, **keywords)
248+
249+
250+
def load_data(afile, file_type=None, **keywords):
251+
if isstream(afile) and file_type is None:
252+
file_type='ods'
253+
return read_data(afile, file_type=file_type, **keywords)
254+
255+
230256
__VERSION__ = "0.0.8"

0 commit comments

Comments
 (0)