unify the read and write interface among pyexcel plugins
This commit is contained in:
parent
44b3bdc5ad
commit
ac0f272897
34
README.rst
34
README.rst
|
@ -61,32 +61,26 @@ Write to an xls file
|
|||
... from StringIO import StringIO
|
||||
... else:
|
||||
... from io import BytesIO as StringIO
|
||||
>>> from pyexcel.ext.xls import OrderedDict
|
||||
>>> from pyexcel_io import OrderedDict
|
||||
|
||||
|
||||
Here's the sample code to write a dictionary to an xls file::
|
||||
|
||||
>>> from pyexcel_xls import XLWriter
|
||||
>>> from pyexcel_xls import store_data
|
||||
>>> data = OrderedDict() # from collections import OrderedDict
|
||||
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
|
||||
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
|
||||
>>> writer = XLWriter("your_file.xls")
|
||||
>>> writer.write(data)
|
||||
>>> writer.close()
|
||||
>>> store_data("your_file.xls", data)
|
||||
|
||||
Read from an xls file
|
||||
**********************
|
||||
|
||||
Here's the sample code::
|
||||
|
||||
>>> from pyexcel_xls import XLBook
|
||||
|
||||
>>> book = XLBook("your_file.xls")
|
||||
>>> # book.sheets() returns a dictionary of all sheet content
|
||||
>>> # the keys represents sheet names
|
||||
>>> # the values are two dimensional array
|
||||
>>> import json
|
||||
>>> print(json.dumps(book.sheets()))
|
||||
>>> from pyexcel_xls import load_data
|
||||
>>> data = load_data("your_file.xls")
|
||||
>>> import json
|
||||
>>> print(json.dumps(data))
|
||||
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
|
||||
|
||||
Write an xls to memory
|
||||
|
@ -94,15 +88,13 @@ Write an xls to memory
|
|||
|
||||
Here's the sample code to write a dictionary to an xls file::
|
||||
|
||||
>>> from pyexcel_xls import XLWriter
|
||||
>>> from pyexcel_xls import store_data
|
||||
>>> data = OrderedDict()
|
||||
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
|
||||
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
|
||||
>>> io = StringIO()
|
||||
>>> writer = XLWriter(io)
|
||||
>>> writer.write(data)
|
||||
>>> writer.close()
|
||||
>>> # do something witht the io
|
||||
>>> writer = store_data(io, data)
|
||||
>>> # do something with the io
|
||||
>>> # In reality, you might give it to your http response
|
||||
>>> # object for downloading
|
||||
|
||||
|
@ -115,8 +107,8 @@ Continue from previous example::
|
|||
>>> # This is just an illustration
|
||||
>>> # In reality, you might deal with xls file upload
|
||||
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
|
||||
>>> book = XLBook(None, io.getvalue())
|
||||
>>> print(json.dumps(book.sheets()))
|
||||
>>> data = load_data(io)
|
||||
>>> print(json.dumps(data))
|
||||
{"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]]}
|
||||
|
||||
|
||||
|
@ -169,7 +161,7 @@ You got to wrap the binary content with stream to get xls working::
|
|||
>>> xlfile = "another_file.xls"
|
||||
>>> with open(xlfile, "rb") as f:
|
||||
... content = f.read()
|
||||
... r = pe.get_book(file_type="xls", content=content)
|
||||
... r = pe.get_book(file_type="xls", file_content=content)
|
||||
... print(r)
|
||||
...
|
||||
Sheet Name: Sheet 1
|
||||
|
|
|
@ -17,12 +17,12 @@ from pyexcel_io import (
|
|||
SheetWriter,
|
||||
BookWriter,
|
||||
READERS,
|
||||
WRITERS
|
||||
WRITERS,
|
||||
isstream,
|
||||
load_data as read_data,
|
||||
store_data as write_data
|
||||
)
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
|
||||
from ordereddict import OrderedDict
|
||||
else:
|
||||
from collections import OrderedDict
|
||||
PY2 = sys.version_info[0] == 2
|
||||
|
||||
|
||||
DEFAULT_DATE_FORMAT = "DD/MM/YY"
|
||||
|
@ -137,7 +137,7 @@ class XLBook(BookReader):
|
|||
on_demand = False
|
||||
if self.sheet_name is not None or self.sheet_index is not None:
|
||||
on_demand = True
|
||||
return xlrd.open_workbook(None, file_contents=file_content,
|
||||
return xlrd.open_workbook(None, file_contents=file_content.getvalue(),
|
||||
on_demand=on_demand)
|
||||
|
||||
def load_from_file(self, filename, **keywords):
|
||||
|
@ -221,4 +221,28 @@ WRITERS.update({
|
|||
})
|
||||
|
||||
|
||||
def is_string(atype):
|
||||
"""find out if a type is str or not"""
|
||||
if atype == str:
|
||||
return True
|
||||
elif PY2:
|
||||
if atype == unicode:
|
||||
return True
|
||||
elif atype == str:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def store_data(afile, data, file_type=None, **keywords):
|
||||
if isstream(afile) and file_type is None:
|
||||
file_type='xls'
|
||||
write_data(afile, data, file_type=file_type, **keywords)
|
||||
|
||||
|
||||
def load_data(afile, file_type=None, **keywords):
|
||||
if isstream(afile) and file_type is None:
|
||||
file_type='xls'
|
||||
return read_data(afile, file_type=file_type, **keywords)
|
||||
|
||||
|
||||
__VERSION__ = "0.0.7"
|
||||
|
|
Loading…
Reference in New Issue