Skip to content

Commit 946d930

Browse files
committed
incomplete changes
1 parent cd56f2c commit 946d930

File tree

5 files changed

+102
-92
lines changed

5 files changed

+102
-92
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ install:
1212
- pip install -r requirements.txt --use-mirrors
1313
- pip install -r tests/requirements.txt --use-mirrors
1414
script:
15-
nosetests --rednose --with-cov
15+
make test
1616
after_success:
1717
codecov

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all: test
2+
3+
test:
4+
bash test.sh
5+

README.rst

Lines changed: 94 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -47,71 +47,73 @@ Usage
4747
As a standalone library
4848
------------------------
4949

50+
.. testcode::
51+
:hide:
52+
53+
>>> import sys
54+
>>> if sys.version_info[0] < 3:
55+
... from StringIO import StringIO
56+
... else:
57+
... from io import BytesIO as StringIO
58+
>>> from pyexcel_xl.xlbook import OrderedDict
59+
60+
Write to an ods file
61+
*********************
62+
63+
Here's the sample code to write a dictionary to an ods file::
64+
65+
>>> from pyexcel_ods3 import ODSWriter
66+
>>> data = OrderedDict()
67+
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
68+
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
69+
>>> writer = ODSWriter("your_file.ods")
70+
>>> writer.write(data)
71+
>>> writer.close()
72+
5073
Read from an ods file
5174
**********************
5275

5376
Here's the sample code::
5477

55-
from pyexcel_ods3 import ODSBook
56-
import json
57-
58-
book = ODSBook("your_file.ods")
59-
# book.sheets() returns a dictionary of all sheet content
60-
# the keys represents sheet names
61-
# the values are two dimensional array
62-
print(book.sheets())
78+
>>> from pyexcel_ods3 import ODSBook
79+
>>> book = ODSBook("your_file.ods")
80+
>>> # book.sheets() returns a dictionary of all sheet content
81+
>>> # the keys represents sheet names
82+
>>> # the values are two dimensional array
83+
>>> import json
84+
>>> print(json.dumps(book.sheets()))
85+
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
6386

64-
Write to an ods file
65-
*********************
87+
Write an ods file to memory
88+
**********************
6689

6790
Here's the sample code to write a dictionary to an ods file::
6891

69-
from pyexcel_ods3 import ODSWriter
92+
>>> from pyexcel_ods3 import ODSWriter
93+
>>> data = OrderedDict()
94+
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
95+
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
96+
>>> io = StringIO()
97+
>>> writer = ODSWriter(io)
98+
>>> writer.write(data)
99+
>>> writer.close()
100+
>>> # do something witht the io
101+
>>> # In reality, you might give it to your http response
102+
>>> # object for downloading
70103

71-
data = {
72-
"Sheet 1": [[1, 2, 3], [4, 5, 6]],
73-
"Sheet 2": [["row 1", "row 2", "row 3"]]
74-
}
75-
writer = ODSWriter("your_file.ods")
76-
writer.write(data)
77-
writer.close()
78104

79105
Read from an ods from memory
80106
*****************************
81107

82108
Here's the sample code::
83109

84-
from pyexcel_ods3 import ODSBook
110+
>>> # This is just an illustration
111+
>>> # In reality, you might deal with xl file upload
112+
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
113+
>>> book = ODSBook(None, io.getvalue())
114+
>>> print(json.dumps(book.sheets()))
115+
{"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]]}
85116

86-
# This is just an illustration
87-
# In reality, you might deal with ods file upload
88-
# where you will read from requests.FILES['YOUR_ODS_FILE']
89-
odsfile = "example.ods"
90-
with open(odsfile, "rb") as f:
91-
content = f.read()
92-
book = ODSBook(None, content)
93-
print(book.sheets())
94-
95-
96-
Write an ods to memory
97-
**********************
98-
99-
Here's the sample code to write a dictionary to an ods file::
100-
101-
from pyexcel_ods3 import ODSWriter
102-
from StringIO import StringIO
103-
104-
data = {
105-
"Sheet 1": [[1, 2, 3], [4, 5, 6]],
106-
"Sheet 2": [["row 1", "row 2", "row 3"]]
107-
}
108-
io = StringIO()
109-
writer = ODSWriter(io)
110-
writer.write(data)
111-
writer.close()
112-
# do something witht the io
113-
# In reality, you might give it to your http response
114-
# object for downloading
115117

116118
As a pyexcel plugin
117119
--------------------
@@ -127,69 +129,70 @@ Reading from an ods file
127129

128130
Here is the sample code::
129131

130-
from pyexcel import Reader
131-
from pyexcel.ext import ods3
132-
from pyexcel.utils import to_array
133-
import json
134-
135-
# "example.ods"
136-
reader = Reader("example.ods")
137-
data = to_array(reader)
138-
print json.dumps(data)
132+
>>> import pyexcel as pe
133+
>>> from pyexcel.ext import ods3
134+
>>> sheet = pe.load_book("your_file.ods")
135+
>>> sheet
136+
Sheet Name: Sheet 1
137+
+---+---+---+
138+
| 1 | 2 | 3 |
139+
+---+---+---+
140+
| 4 | 5 | 6 |
141+
+---+---+---+
142+
Sheet Name: Sheet 2
143+
+-------+-------+-------+
144+
| row 1 | row 2 | row 3 |
145+
+-------+-------+-------+
139146

140147
Writing to an ods file
141148
**********************
142149

143150
Here is the sample code::
144151

145-
from pyexcel import Writer
146-
from pyexcel.ext import ods3
147-
148-
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
149-
writer = Writer("output.ods")
150-
writer.write_array(array)
151-
writer.close()
152+
>>> sheet.save_as("another_file.ods")
152153

153154
Reading from a StringIO instance
154155
================================
155156

156157
You got to wrap the binary content with StringIO to get odf working::
157158

158159

159-
import pyexcel
160-
from pyexcel.ext import ods3
161-
from StringIO import StringIO # for py3, from io import BytesIO as StringIO
162-
163-
# This is just an illustration
164-
# In reality, you might deal with ods file upload
165-
# where you will read from requests.FILES['YOUR_ODS_FILE']
166-
odsfile = "example.ods"
167-
with open(odsfile, "rb") as f:
168-
content = f.read()
169-
r = pyexcel.Reader(("ods", StringIO(content)))
160+
>>> # This is just an illustration
161+
>>> # In reality, you might deal with xl file upload
162+
>>> # where you will read from requests.FILES['YOUR_XL_FILE']
163+
>>> xlfile = "another_file.ods"
164+
>>> with open(xlfile, "rb") as f:
165+
... content = f.read()
166+
... r = pe.load_book_from_memory("ods", content)
167+
... print(r)
168+
...
169+
Sheet Name: Sheet 1
170+
+---+---+---+
171+
| 1 | 2 | 3 |
172+
+---+---+---+
173+
| 4 | 5 | 6 |
174+
+---+---+---+
175+
Sheet Name: Sheet 2
176+
+-------+-------+-------+
177+
| row 1 | row 2 | row 3 |
178+
+-------+-------+-------+
170179

171180

172181
Writing to a StringIO instance
173182
================================
174183

175184
You need to pass a StringIO instance to Writer::
176185

177-
import pyexcel
178-
from pyexcel.ext import ods3
179-
from StringIO import StringIO # for py3, from io import BytesIO as StringIO
180-
181-
182-
data = [
183-
[1, 2, 3],
184-
[4, 5, 6]
185-
]
186-
io = StringIO()
187-
w = pyexcel.Writer(("ods",io))
188-
w.write_rows(data)
189-
w.close()
190-
# then do something with io
191-
# In reality, you might give it to your http response
192-
# object for downloading
186+
>>> data = [
187+
... [1, 2, 3],
188+
... [4, 5, 6]
189+
... ]
190+
>>> io = StringIO()
191+
>>> sheet = pe.Sheet(data)
192+
>>> sheet.save_to_memory("ods", io)
193+
>>> # then do something with io
194+
>>> # In reality, you might give it to your http response
195+
>>> # object for downloading
193196

194197

195198
Dependencies

test.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nosetests --with-cov --with-doctest --doctest-extension=.rst

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nosetests --rednose --with-cov --with-doctest --doctest-extension=.rst

0 commit comments

Comments
 (0)