Skip to content

Commit 3bf8c2d

Browse files
committed
unify the read and write interface among pyexcel plugins
1 parent 08b232e commit 3bf8c2d

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

README.rst

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,39 @@ Write to an xlsx file
6262
... from StringIO import StringIO
6363
... else:
6464
... from io import BytesIO as StringIO
65-
>>> from pyexcel_xlsx import OrderedDict
65+
>>> from pyexcel_io import OrderedDict
6666

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

69-
>>> from pyexcel_xlsx import XLSXWriter
69+
>>> from pyexcel_xlsx 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 = XLSXWriter("your_file.xlsx")
74-
>>> writer.write(data)
75-
>>> writer.close()
73+
>>> store_data("your_file.xlsx", data)
7674

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

8078
Here's the sample code::
8179

82-
>>> from pyexcel_xlsx import XLSXBook
83-
84-
>>> book = XLSXBook("your_file.xlsx")
85-
>>> # book.sheets() returns a dictionary of all sheet content
86-
>>> # the keys represents sheet names
87-
>>> # the values are two dimensional array
80+
>>> from pyexcel_xlsx import load_data
81+
>>> data = load_data("your_file.xlsx")
8882
>>> import json
89-
>>> print(json.dumps(book.sheets()))
83+
>>> print(json.dumps(data))
9084
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
9185

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

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

97-
>>> from pyexcel_xlsx import XLSXWriter
91+
>>> from pyexcel_xlsx 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 = XLSXWriter(io)
103-
>>> writer.write(data)
104-
>>> writer.close()
105-
>>> # do something witht the io
96+
>>> 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 xlsx file upload
117109
>>> # where you will read from requests.FILES['YOUR_XLSX_FILE']
118-
>>> book = XLSXBook(None, io.getvalue())
119-
>>> print(json.dumps(book.sheets()))
110+
>>> data = load_data(io)
111+
>>> print(json.dumps(data))
120112
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [[7, 8, 9], [10, 11, 12]]}
121113

122114

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

pyexcel_xlsx/__init__.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
SheetWriter,
1616
BookWriter,
1717
READERS,
18-
WRITERS
18+
WRITERS,
19+
isstream,
20+
load_data as read_data,
21+
store_data as write_data
1922
)
20-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
21-
from ordereddict import OrderedDict
22-
else:
23-
from collections import OrderedDict
24-
if sys.version_info[0] < 3:
25-
from StringIO import StringIO
26-
else:
27-
from io import BytesIO as StringIO
28-
23+
PY2 = sys.version_info[0] == 2
24+
2925

3026
COLUMNS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
3127
COLUMN_LENGTH = 26
@@ -77,7 +73,7 @@ def get_sheet(self, nativeSheet):
7773
return XLSXSheet(nativeSheet)
7874

7975
def load_from_memory(self, file_content, **keywords):
80-
return openpyxl.load_workbook(filename=StringIO(file_content),
76+
return openpyxl.load_workbook(filename=file_content,
8177
data_only=True)
8278

8379
def load_from_file(self, filename, **keywords):
@@ -155,5 +151,28 @@ def close(self):
155151
"xlsx": XLSXWriter
156152
})
157153

158-
154+
def is_string(atype):
155+
"""find out if a type is str or not"""
156+
if atype == str:
157+
return True
158+
elif PY2:
159+
if atype == unicode:
160+
return True
161+
elif atype == str:
162+
return True
163+
return False
164+
165+
166+
def store_data(afile, data, file_type=None, **keywords):
167+
if isstream(afile) and file_type is None:
168+
file_type='xlsx'
169+
write_data(afile, data, file_type=file_type, **keywords)
170+
171+
172+
def load_data(afile, file_type=None, **keywords):
173+
if isstream(afile) and file_type is None:
174+
file_type='xlsx'
175+
return read_data(afile, file_type=file_type, **keywords)
176+
177+
159178
__VERSION__ = "0.0.6"

0 commit comments

Comments
 (0)