@@ -47,71 +47,73 @@ Usage
47
47
As a standalone library
48
48
------------------------
49
49
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
+
50
73
Read from an ods file
51
74
**********************
52
75
53
76
Here's the sample code::
54
77
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"]]}
63
86
64
- Write to an ods file
65
- *********************
87
+ Write an ods file to memory
88
+ **********************
66
89
67
90
Here's the sample code to write a dictionary to an ods file::
68
91
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
70
103
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()
78
104
79
105
Read from an ods from memory
80
106
*****************************
81
107
82
108
Here's the sample code::
83
109
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]]}
85
116
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
115
117
116
118
As a pyexcel plugin
117
119
--------------------
@@ -127,69 +129,70 @@ Reading from an ods file
127
129
128
130
Here is the sample code::
129
131
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
+ +-------+-------+-------+
139
146
140
147
Writing to an ods file
141
148
**********************
142
149
143
150
Here is the sample code::
144
151
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")
152
153
153
154
Reading from a StringIO instance
154
155
================================
155
156
156
157
You got to wrap the binary content with StringIO to get odf working::
157
158
158
159
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
+ +-------+-------+-------+
170
179
171
180
172
181
Writing to a StringIO instance
173
182
================================
174
183
175
184
You need to pass a StringIO instance to Writer::
176
185
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
193
196
194
197
195
198
Dependencies
0 commit comments