Repository navigation
Expand file tree
/
Copy pathworkbook.html
More file actions
468 lines (446 loc) · 29.4 KB
/
Copy pathworkbook.html
File metadata and controls
468 lines (446 loc) · 29.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Workbook Class — XlsxWriter Documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.3.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="XlsxWriter Documentation" href="index.html" />
<link rel="next" title="The Worksheet Class" href="worksheet.html" />
<link rel="prev" title="Tutorial 3: Writing different types of data to the XLSX File" href="tutorial03.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="worksheet.html" title="The Worksheet Class"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial03.html" title="Tutorial 3: Writing different types of data to the XLSX File"
accesskey="P">previous</a> |</li>
<li><a href="index.html">XlsxWriter Documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="the-workbook-class">
<span id="workbook"></span><h1>The Workbook Class</h1>
<p>The Workbook class is the main class exposed by the XlsxWriter module and it is
the only class that you will need to instantiate directly.</p>
<p>The Workbook class represents the entire spreadsheet as you see it in Excel and
internally it represents the Excel file as it is written on disk.</p>
<div class="section" id="constructor">
<h2>Constructor</h2>
<dl class="function">
<dt id="Workbook">
<tt class="descname">Workbook</tt><big>(</big><em>filename</em><span class="optional">[</span>, <em>options</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Create a new XlsxWriter Workbook object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>filename</strong> (<a class="reference external" href="http://docs.python.org/2/library/string.html#string" title="(in Python v2.7)"><em>string</em></a>) – The name of the new Excel file to create.</li>
<li><strong>options</strong> (<a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#dict" title="(in Python v2.7)"><em>dict</em></a>) – Optional workbook parameters. See below.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">A Workbook object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">Workbook()</span></tt> constructor is used to create a new Excel workbook with a
given filename:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">xlsxwriter</span> <span class="kn">import</span> <span class="n">Workbook</span>
<span class="n">workbook</span> <span class="o">=</span> <span class="n">Workbook</span><span class="p">(</span><span class="s">'filename.xlsx'</span><span class="p">)</span>
<span class="n">worksheet</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_worksheet</span><span class="p">()</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s">'Hello Excel'</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/workbook01.png" src="_images/workbook01.png" />
<p>The constructor options are:</p>
<ul>
<li><p class="first"><strong>constant_memory</strong>: Reduces the amount of data stored in memory so that
large files can be written efficiently:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span> <span class="o">=</span> <span class="n">Workbook</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="p">{</span><span class="s">'constant_memory'</span><span class="p">:</span> <span class="bp">True</span><span class="p">})</span>
</pre></div>
</div>
<p>Note, in this mode a row of data is written and then discarded when a cell
in a new row is added via one of the worksheet <tt class="docutils literal"><span class="pre">write_()</span></tt> methods. As such
data should be written in sequential row order once this mode is on.</p>
<p>See <a class="reference internal" href="working_with_memory.html#memory-perf"><em>Working with Memory and Performance</em></a> for more details.</p>
</li>
<li><p class="first"><strong>tmpdir</strong>: <tt class="docutils literal"><span class="pre">XlsxWriter</span></tt> stores worksheet data in a temporary directory
prior to assembling the final XLSX file. The temporary directory is
generated by the <a class="reference external" href="http://docs.python.org/2/library/tempfile.html#tempfile.mkdtemp" title="(in Python v2.7)"><tt class="xref py py-func docutils literal"><span class="pre">tempfile.mkdtemp()</span></tt></a> function which in general defaults
to using your system’s temporary directory. If the default temporary
directory isn’t accessible to your application, or doesn’t contain enough
space, you can specify an alternative location using the <tt class="docutils literal"><span class="pre">tempdir</span></tt> option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span> <span class="o">=</span> <span class="n">Workbook</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="p">{</span><span class="s">'tmpdir'</span><span class="p">:</span> <span class="s">'/home/user/tmp'</span><span class="p">})</span>
</pre></div>
</div>
<p>The temporary directory must exist and will not be created.</p>
</li>
<li><p class="first"><strong>date_1904</strong>: Excel for Windows uses a default epoch of 1900 and Excel for
Mac uses an epoch of 1904. However, Excel on either platform will convert
automatically between one system and the other. XlsxWriter stores dates in
the 1900 format by default. If you wish to change this you can use the
<tt class="docutils literal"><span class="pre">date_1904</span></tt> workbook option. This option is mainly for backward
compatibility with Excel::Writer::XLSX and in general isn’t required very
often:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span> <span class="o">=</span> <span class="n">Workbook</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="p">{</span><span class="s">'date_1904'</span><span class="p">:</span> <span class="bp">True</span><span class="p">})</span>
</pre></div>
</div>
</li>
</ul>
<p>When specifying a filename it is recommended that you use an <tt class="docutils literal"><span class="pre">.xlsx</span></tt>
extension or Excel will generate a warning opening the file.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">A later version of the module will support writing to filehandles like
<a class="reference internal" href="excel_writer_xlsx.html#ewx"><em>Excel::Writer::XLSX</em></a>.</p>
</div>
</div>
<div class="section" id="workbook-add-worksheet">
<h2>workbook.add_worksheet()</h2>
<dl class="function">
<dt id="add_worksheet">
<tt class="descname">add_worksheet</tt><big>(</big><span class="optional">[</span><em>sheetname</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Add a new worksheet to a workbook.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>sheetname</strong> (<a class="reference external" href="http://docs.python.org/2/library/string.html#string" title="(in Python v2.7)"><em>string</em></a>) – Optional worksheet name, defaults to Sheet1, etc.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">A <a class="reference internal" href="worksheet.html#worksheet"><em>worksheet</em></a> object.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">add_worksheet()</span></tt> method adds a new worksheet to a workbook.</p>
<p>At least one worksheet should be added to a new workbook. The
<a class="reference internal" href="worksheet.html#worksheet"><em>Worksheet</em></a> object is used to write data and configure a
worksheet in the workbook.</p>
<p>The <tt class="docutils literal"><span class="pre">sheetname</span></tt> parameter is optional. If it is not specified the default
Excel convention will be followed, i.e. Sheet1, Sheet2, etc.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">worksheet1</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_worksheet</span><span class="p">()</span> <span class="c"># Sheet1</span>
<span class="n">worksheet2</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_worksheet</span><span class="p">(</span><span class="s">'Foglio2'</span><span class="p">)</span> <span class="c"># Foglio2</span>
<span class="n">worksheet3</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_worksheet</span><span class="p">(</span><span class="s">'Data'</span><span class="p">)</span> <span class="c"># Data</span>
<span class="n">worksheet4</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_worksheet</span><span class="p">()</span> <span class="c"># Sheet4</span>
</pre></div>
</div>
<img alt="_images/workbook02.png" src="_images/workbook02.png" />
<p>The worksheet name must be a valid Excel worksheet name, i.e. it cannot contain
any of the characters <tt class="docutils literal"><span class="pre">'</span> <span class="pre">[</span> <span class="pre">]</span> <span class="pre">:</span> <span class="pre">*</span> <span class="pre">?</span> <span class="pre">/</span> <span class="pre">\</span>
<span class="pre">'</span></tt> and it must be less than 32 characters. In addition, you cannot use the same, case insensitive, <tt class="docutils literal"><span class="pre">sheetname</span></tt> for more than one worksheet.</p>
</div>
<div class="section" id="workbook-add-format">
<h2>workbook.add_format()</h2>
<dl class="function">
<dt id="add_format">
<tt class="descname">add_format</tt><big>(</big><span class="optional">[</span><em>properties</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Create a new Format object to formats cells in worksheets.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>properties</strong> (<em>dictionary</em>) – An optional dictionary of format properties.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">A <a class="reference internal" href="format.html#format"><em>format</em></a> object.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">add_format()</span></tt> method can be used to create new <a class="reference internal" href="format.html#format"><em>Format</em></a>
objects which are used to apply formatting to a cell. You can either define
the properties at creation time via a dictionary of property values or later
via method calls:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format1</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_format</span><span class="p">(</span><span class="n">props</span><span class="p">);</span> <span class="c"># Set properties at creation.</span>
<span class="n">format2</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_format</span><span class="p">();</span> <span class="c"># Set properties later.</span>
</pre></div>
</div>
<p>See the <a class="reference internal" href="format.html#format"><em>The Format Class</em></a> and <a class="reference internal" href="working_with_formats.html#working-with-formats"><em>Working with Formats</em></a> sections for more details
about Format properties and how to set them.</p>
</div>
<div class="section" id="workbook-add-chart">
<h2>workbook.add_chart()</h2>
<dl class="function">
<dt id="add_chart">
<tt class="descname">add_chart</tt><big>(</big><em>options</em><big>)</big></dt>
<dd><p>Create a chart object that can be added to a worksheet.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>options</strong> (<em>dictionary</em>) – An dictionary of chart type options.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">A <a class="reference internal" href="chart.html#chart-class"><em>Chart</em></a> object.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This method is use to create a new chart object that can be inserted into a
worksheet via the <a class="reference internal" href="worksheet.html#insert_chart" title="insert_chart"><tt class="xref py py-func docutils literal"><span class="pre">insert_chart()</span></tt></a> Worksheet method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">chart</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_chart</span><span class="p">({</span><span class="s">'type'</span><span class="p">:</span> <span class="s">'column'</span><span class="p">})</span>
</pre></div>
</div>
<p>The properties that can be set are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nb">type</span> <span class="p">(</span><span class="n">required</span><span class="p">)</span>
<span class="n">subtype</span> <span class="p">(</span><span class="n">optional</span><span class="p">)</span>
</pre></div>
</div>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">type</span></tt></p>
<p>This is a required parameter. It defines the type of chart that will be
created:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">chart</span> <span class="o">=</span> <span class="n">workbook</span><span class="o">.</span><span class="n">add_chart</span><span class="p">({</span><span class="s">'type'</span><span class="p">:</span> <span class="s">'line'</span><span class="p">})</span>
</pre></div>
</div>
<p>The available types are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">area</span>
<span class="n">bar</span>
<span class="n">column</span>
<span class="n">line</span>
<span class="n">pie</span>
<span class="n">radar</span>
<span class="n">scatter</span>
<span class="n">stock</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">subtype</span></tt></p>
<p>Used to define a chart subtype where available:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span><span class="o">.</span><span class="n">add_chart</span><span class="p">({</span><span class="s">'type'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">,</span> <span class="s">'subtype'</span><span class="p">:</span> <span class="s">'stacked'</span><span class="p">})</span>
</pre></div>
</div>
</li>
</ul>
<p>See the <a class="reference internal" href="chart.html#chart-class"><em>The Chart Class</em></a> for a list of available chart subtypes.</p>
<p>See also <a class="reference internal" href="working_with_charts.html#working-with-charts"><em>Working with Charts</em></a> and <a class="reference internal" href="chart_examples.html#chart-examples"><em>Chart Examples</em></a>.</p>
</div>
<div class="section" id="workbook-close">
<h2>workbook.close()</h2>
<dl class="function">
<dt id="close">
<tt class="descname">close</tt><big>(</big><big>)</big></dt>
<dd><p>Close the Workbook object and write the XLSX file.</p>
</dd></dl>
<p>In general your Excel file will be closed automatically when your program ends
or when the Workbook object goes out of scope, however the <tt class="docutils literal"><span class="pre">close()</span></tt> method
can be used to explicitly close an Excel file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>An explicit <tt class="docutils literal"><span class="pre">close()</span></tt> is required if the file must be closed prior to
performing some external action on it such as copying it, reading its size or
attaching it to an email.</p>
<p>In addition, <tt class="docutils literal"><span class="pre">close()</span></tt> may be occasionally required to prevent Python’s
garbage collector from disposing of the Workbook, Worksheet and Format objects
in the wrong order.</p>
<p>In general, if an XlsxWriter file is created with a size of 0 bytes or fails to
be created for some unknown silent reason you should add <tt class="docutils literal"><span class="pre">close()</span></tt> to your
program.</p>
</div>
<div class="section" id="workbook-set-properties">
<h2>workbook.set_properties()</h2>
<dl class="function">
<dt id="set_properties">
<tt class="descname">set_properties</tt><big>(</big><big>)</big></dt>
<dd><p>Set the document properties such as Title, Author etc.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>properties</strong> (<a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#dict" title="(in Python v2.7)"><em>dict</em></a>) – Dictionary of document properties.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">set_properties</span></tt> method can be used to set the document properties of the
Excel file created by <tt class="docutils literal"><span class="pre">XlsxWriter</span></tt>. These properties are visible when you
use the <tt class="docutils literal"><span class="pre">Office</span> <span class="pre">Button</span> <span class="pre">-></span> <span class="pre">Prepare</span> <span class="pre">-></span> <span class="pre">Properties</span></tt> option in Excel and are
also available to external applications that read or index windows files.</p>
<p>The properties that can be set are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">title</span></tt></li>
<li><tt class="docutils literal"><span class="pre">subject</span></tt></li>
<li><tt class="docutils literal"><span class="pre">author</span></tt></li>
<li><tt class="docutils literal"><span class="pre">manager</span></tt></li>
<li><tt class="docutils literal"><span class="pre">company</span></tt></li>
<li><tt class="docutils literal"><span class="pre">category</span></tt></li>
<li><tt class="docutils literal"><span class="pre">keywords</span></tt></li>
<li><tt class="docutils literal"><span class="pre">comments</span></tt></li>
<li><tt class="docutils literal"><span class="pre">status</span></tt></li>
</ul>
<p>The properties should be passed in dictionary format as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span><span class="o">.</span><span class="n">set_properties</span><span class="p">({</span>
<span class="s">'title'</span><span class="p">:</span> <span class="s">'This is an example spreadsheet'</span><span class="p">,</span>
<span class="s">'subject'</span><span class="p">:</span> <span class="s">'With document properties'</span><span class="p">,</span>
<span class="s">'author'</span><span class="p">:</span> <span class="s">'John McNamara'</span><span class="p">,</span>
<span class="s">'manager'</span><span class="p">:</span> <span class="s">'Dr. Heinz Doofenshmirtz'</span><span class="p">,</span>
<span class="s">'company'</span><span class="p">:</span> <span class="s">'of Wolves'</span><span class="p">,</span>
<span class="s">'category'</span><span class="p">:</span> <span class="s">'Example spreadsheets'</span><span class="p">,</span>
<span class="s">'keywords'</span><span class="p">:</span> <span class="s">'Sample, Example, Properties'</span><span class="p">,</span>
<span class="s">'comments'</span><span class="p">:</span> <span class="s">'Created with Python and XlsxWriter'</span><span class="p">})</span>
</pre></div>
</div>
<img alt="_images/doc_properties.png" src="_images/doc_properties.png" />
<p>See also <a class="reference internal" href="example_doc_properties.html#ex-doc-properties"><em>Example: Setting Document Properties</em></a>.</p>
</div>
<div class="section" id="workbook-define-name">
<h2>workbook.define_name()</h2>
<dl class="function">
<dt id="define_name">
<tt class="descname">define_name</tt><big>(</big><big>)</big></dt>
<dd><p>Create a defined name in the workbook to use as a variable.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>name</strong> (<a class="reference external" href="http://docs.python.org/2/library/string.html#string" title="(in Python v2.7)"><em>string</em></a>) – The defined name.</li>
<li><strong>formula</strong> (<a class="reference external" href="http://docs.python.org/2/library/string.html#string" title="(in Python v2.7)"><em>string</em></a>) – The cell or range that the defined name refers to.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This method is used to defined a name that can be used to represent a value, a
single cell or a range of cells in a workbook.</p>
<p>For example to set a global/workbook name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Global/workbook names.</span>
<span class="n">workbook</span><span class="o">.</span><span class="n">define_name</span><span class="p">(</span><span class="s">'Exchange_rate'</span><span class="p">,</span> <span class="s">'=0.96'</span><span class="p">)</span>
<span class="n">workbook</span><span class="o">.</span><span class="n">define_name</span><span class="p">(</span><span class="s">'Sales'</span><span class="p">,</span> <span class="s">'=Sheet1!$G$1:$H$10'</span><span class="p">)</span>
</pre></div>
</div>
<p>It is also possible to define a local/worksheet name by prefixing it with the
sheet name using the syntax <tt class="docutils literal"><span class="pre">'sheetname!definedname'</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Local/worksheet name.</span>
<span class="n">workbook</span><span class="o">.</span><span class="n">define_name</span><span class="p">(</span><span class="s">'Sheet2!Sales'</span><span class="p">,</span> <span class="s">'=Sheet2!$G$1:$G$10'</span><span class="p">)</span>
</pre></div>
</div>
<p>If the sheet name contains spaces or special characters you must enclose it in
single quotes like in Excel:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">workbook</span><span class="o">.</span><span class="n">define_name</span><span class="p">(</span><span class="s">"'New Data'!Sales"</span><span class="p">,</span> <span class="s">'=Sheet2!$G$1:$G$10'</span><span class="p">)</span>
</pre></div>
</div>
<p>See also the <tt class="docutils literal"><span class="pre">defined_name.py</span></tt> program in the examples directory.</p>
</div>
<div class="section" id="workbook-worksheets">
<h2>workbook.worksheets()</h2>
<dl class="function">
<dt id="worksheets">
<tt class="descname">worksheets</tt><big>(</big><big>)</big></dt>
<dd><p>Return a list of the worksheet objects in the workbook.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">A list of <a class="reference internal" href="worksheet.html#worksheet"><em>worksheet</em></a> objects.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">worksheets()</span></tt> method returns a list of the worksheets in a workbook.
This is useful if you want to repeat an operation on each worksheet in a
workbook:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">worksheet</span> <span class="ow">in</span> <span class="n">workbook</span><span class="o">.</span><span class="n">worksheets</span><span class="p">():</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'A1'</span><span class="p">,</span> <span class="s">'Hello'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">The Workbook Class</a><ul>
<li><a class="reference internal" href="#constructor">Constructor</a></li>
<li><a class="reference internal" href="#workbook-add-worksheet">workbook.add_worksheet()</a></li>
<li><a class="reference internal" href="#workbook-add-format">workbook.add_format()</a></li>
<li><a class="reference internal" href="#workbook-add-chart">workbook.add_chart()</a></li>
<li><a class="reference internal" href="#workbook-close">workbook.close()</a></li>
<li><a class="reference internal" href="#workbook-set-properties">workbook.set_properties()</a></li>
<li><a class="reference internal" href="#workbook-define-name">workbook.define_name()</a></li>
<li><a class="reference internal" href="#workbook-worksheets">workbook.worksheets()</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="tutorial03.html"
title="previous chapter">Tutorial 3: Writing different types of data to the XLSX File</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="worksheet.html"
title="next chapter">The Worksheet Class</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/workbook.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="worksheet.html" title="The Worksheet Class"
>next</a> |</li>
<li class="right" >
<a href="tutorial03.html" title="Tutorial 3: Writing different types of data to the XLSX File"
>previous</a> |</li>
<li><a href="index.html">XlsxWriter Documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2013, John McNamara.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>