forked from jmcnamara/XlsxWriter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkbook.html
More file actions
255 lines (237 loc) · 13.5 KB
/
workbook.html
File metadata and controls
255 lines (237 loc) · 13.5 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
<!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.0.2',
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><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"><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.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">A Workbook object.</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>It is recommended that you always use an <tt class="docutils literal"><span class="pre">.xlsx</span></tt> extension in the filename
or Excel will generate a warning when the file is opened.</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 Worksheet 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></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 Format 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-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>
</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-close">workbook.close()</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>