@@ -23,6 +23,127 @@ Installation
23
23
24
24
{%include "installatio
D7AE
n.rst.jj2" %}
25
25
26
+ Suppose you have the following data in a dictionary:
27
+
28
+ ========= ====
29
+ Name Age
30
+ ========= ====
31
+ Adam 28
32
+ Beatrice 29
33
+ Ceri 30
34
+ Dean 26
35
+ ========= ====
36
+
37
+ you can easily save it into an excel file using the following code:
38
+
39
+ .. code-block:: python
40
+
41
+ >>> import pyexcel
42
+ >>> # make sure you had pyexcel-xls installed
43
+ >>> a_list_of_dictionaries = [
44
+ ... {
45
+ ... "Name": 'Adam',
46
+ ... "Age": 28
47
+ ... },
48
+ ... {
49
+ ... "Name": 'Beatrice',
50
+ ... "Age": 29
51
+ ... },
52
+ ... {
53
+ ... "Name": 'Ceri',
54
+ ... "Age": 30
55
+ ... },
56
+ ... {
57
+ ... "Name": 'Dean',
58
+ ... "Age": 26
59
+ ... }
60
+ ... ]
61
+ >>> pyexcel.save_as(records=a_list_of_dictionaries, dest_file_name="your_file.xls")
62
+
63
+ And here's how to obtain the records:
64
+
65
+ .. code-block:: python
66
+
67
+ >>> import pyexcel as p
68
+ >>> records = p.iget_records(file_name="your_file.xls")
69
+ >>> for record in records:
70
+ ... print("%s is aged at %d" % (record['Name'], record['Age']))
71
+ Adam is aged at 28
72
+ Beatrice is aged at 29
73
+ Ceri is aged at 30
74
+ Dean is aged at 26
75
+ >>> p.free_resources()
76
+
77
+
78
+ Custom data rendering:
79
+
80
+ .. code-block:: python
81
+
82
+ >>> # pip install pyexcel-text==0.2.7.1
83
+ >>> import pyexcel as p
84
+ >>> ccs_insight2 = p.Sheet()
85
+ >>> ccs_insight2.name = "Worldwide Mobile Phone Shipments (Billions), 2017-2021"
86
+ >>> ccs_insight2.ndjson = """
87
+ ... {"year": ["2017", "2018", "2019", "2020", "2021"]}
88
+ ... {"smart phones": [1.53, 1.64, 1.74, 1.82, 1.90]}
89
+ ... {"feature phones": [0.46, 0.38, 0.30, 0.23, 0.17]}
90
+ ... """.strip()
91
+ >>> ccs_insight2
92
+ pyexcel sheet:
93
+ +----------------+------+------+------+------+------+
94
+ | year | 2017 | 2018 | 2019 | 2020 | 2021 |
95
+ +----------------+------+------+------+------+------+
96
+ | smart phones | 1.53 | 1.64 | 1.74 | 1.82 | 1.9 |
97
+ +----------------+------+------+------+------+------+
98
+ | feature phones | 0.46 | 0.38 | 0.3 | 0.23 | 0.17 |
99
+ +----------------+------+------+------+------+------+
100
+
101
+
102
+ Advanced usage :fire:
103
+ ----------------------
104
+
105
+ If you are dealing with big data, please consider these usages:
106
+
107
+ .. code-block:: python
108
+
109
+ >>> def increase_everyones_age(generator):
110
+ ... for row in generator:
111
+ ... row['Age'] += 1
112
+ ... yield row
113
+ >>> def duplicate_each_record(generator):
114
+ ... for row in generator:
115
+ ... yield row
116
+ ... yield row
117
+ >>> records = p.iget_records(file_name="your_file.xls")
118
+ >>> io=p.isave_as(records=duplicate_each_record(increase_everyones_age(records)),
119
+ ... dest_file_type='csv', dest_lineterminator='\n')
120
+ >>> print(io.getvalue())
121
+ Age,Name
122
+ 29,Adam
123
+ 29,Adam
124
+ 30,Beatrice
125
+ 30,Beatrice
126
+ 31,Ceri
127
+ 31,Ceri
128
+ 27,Dean
129
+ 27,Dean
130
+ <BLANKLINE>
131
+
132
+
133
+ Two advantages of above method:
134
+
135
+ #. Add as many wrapping functions as you want.
136
+ #. Constant memory consumption
137
+
138
+
139
+ .. testcode::
140
+ :hide:
141
+
142
+ >>> import os
143
+ >>> os.unlink("your_file.xls")
144
+
145
+
146
+
26
147
For individual excel file formats, please install them as you wish:
27
148
28
149
{%include "plugins-list.rst.jj2"%}
0 commit comments