forked from jmcnamara/XlsxWriter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.html
More file actions
1251 lines (1179 loc) · 68.9 KB
/
format.html
File metadata and controls
1251 lines (1179 loc) · 68.9 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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 Format 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="Working with Cell Notation" href="working_with_cell_notation.html" />
<link rel="prev" title="The Worksheet Class (Page Setup)" href="page_setup.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="working_with_cell_notation.html" title="Working with Cell Notation"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="page_setup.html" title="The Worksheet Class (Page Setup)"
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-format-class">
<span id="format"></span><h1>The Format Class</h1>
<p>This section describes the methods and properties that are available for
formatting cells in Excel.</p>
<p>The properties of a cell that can be formatted include: fonts, colours,
patterns, borders, alignment and number formatting.</p>
<img alt="_images/formats_intro.png" src="_images/formats_intro.png" />
<div class="section" id="format-set-font-name">
<h2>format.set_font_name()</h2>
<dl class="function">
<dt id="set_font_name">
<tt class="descname">set_font_name</tt><big>(</big><em>fontname</em><big>)</big></dt>
<dd><p>Set the font used in the cell.</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>fontname</strong> (<a class="reference external" href="http://docs.python.org/2/library/string.html#string" title="(in Python v2.7)"><em>string</em></a>) – Cell font.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Specify the font used used in the cell format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">cell_format</span><span class="o">.</span><span class="n">set_font_name</span><span class="p">(</span><span class="s">'Times New Roman'</span><span class="p">)</span>
</pre></div>
</div>
<p>Excel can only display fonts that are installed on the system that it is
running on. Therefore it is best to use the fonts that come as standard such
as ‘Calibri’, ‘Times New Roman’ and ‘Courier New’.</p>
<p>The default font for an unformatted cell in Excel 2007+ is ‘Calibri’.</p>
</div>
<div class="section" id="format-set-font-size">
<h2>format.set_font_size()</h2>
<dl class="function">
<dt id="set_font_size">
<tt class="descname">set_font_size</tt><big>(</big><em>size</em><big>)</big></dt>
<dd><p>Set the size of the font used in the cell.</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>size</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – The cell font size.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the font size of the cell format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_font_size</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
</pre></div>
</div>
<p>Excel adjusts the height of a row to accommodate the largest font size in the
row. You can also explicitly specify the height of a row using the
<a class="reference internal" href="worksheet.html#set_row" title="set_row"><tt class="xref py py-func docutils literal"><span class="pre">set_row()</span></tt></a> worksheet method.</p>
</div>
<div class="section" id="format-set-font-color">
<h2>format.set_font_color()</h2>
<dl class="function">
<dt id="set_font_color">
<tt class="descname">set_font_color</tt><big>(</big><em>color</em><big>)</big></dt>
<dd><p>Set the color of the font used in the cell.</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>color</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 font color.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the font colour:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_font_color</span><span class="p">(</span><span class="s">'red'</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">'wheelbarrow'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>The color can be a Html style <tt class="docutils literal"><span class="pre">#RRGGBB</span></tt> string or a limited number of named
colors, see <a class="reference internal" href="working_with_formats.html#format-colors"><em>Format Colors</em></a>.</p>
<p>Note: The <tt class="docutils literal"><span class="pre">set_font_color()</span></tt> method is used to set the colour of the font in
a cell. To set the colour of a cell use the <a class="reference internal" href="#set_bg_color" title="set_bg_color"><tt class="xref py py-func docutils literal"><span class="pre">set_bg_color()</span></tt></a> and
<a class="reference internal" href="#set_pattern" title="set_pattern"><tt class="xref py py-func docutils literal"><span class="pre">set_pattern()</span></tt></a> methods.</p>
</div>
<div class="section" id="format-set-bold">
<h2>format.set_bold()</h2>
<dl class="function">
<dt id="set_bold">
<tt class="descname">set_bold</tt><big>(</big><big>)</big></dt>
<dd><p>Turn on bold for the format font.</p>
</dd></dl>
<p>Set the bold property of the font:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</span><span class="o">.</span><span class="n">set_bold</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="format-set-italic">
<h2>format.set_italic()</h2>
<dl class="function">
<dt id="set_italic">
<tt class="descname">set_italic</tt><big>(</big><big>)</big></dt>
<dd><p>Turn on italic for the format font.</p>
</dd></dl>
<p>Set the italic property of the font:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</span><span class="o">.</span><span class="n">set_italic</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="format-set-underline-style">
<h2>format.set_underline(style)</h2>
<dl class="function">
<dt id="set_underline">
<tt class="descname">set_underline</tt><big>(</big><big>)</big></dt>
<dd><p>Turn on underline for the format.</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>style</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Underline style.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the underline property of the format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</span><span class="o">.</span><span class="n">set_underline</span><span class="p">()</span>
</pre></div>
</div>
<p>The available underline styles are:</p>
<ul class="simple">
<li>1 = Single underline (the default)</li>
<li>2 = Double underline</li>
<li>33 = Single accounting underline</li>
<li>34 = Double accounting underline</li>
</ul>
</div>
<div class="section" id="format-set-font-strikeout">
<h2>format.set_font_strikeout()</h2>
<dl class="function">
<dt id="set_font_strikeout">
<tt class="descname">set_font_strikeout</tt><big>(</big><big>)</big></dt>
<dd><p>Set the strikeout property of the font.</p>
</dd></dl>
</div>
<div class="section" id="format-set-font-script">
<h2>format.set_font_script()</h2>
<dl class="function">
<dt id="set_font_script">
<tt class="descname">set_font_script</tt><big>(</big><big>)</big></dt>
<dd><p>Set the superscript/subscript property of the font.</p>
</dd></dl>
<p>The available options are:</p>
<ul class="simple">
<li>1 = Superscript</li>
<li>2 = Subscript</li>
</ul>
</div>
<div class="section" id="format-set-num-format">
<h2>format.set_num_format()</h2>
<dl class="function">
<dt id="set_num_format">
<tt class="descname">set_num_format</tt><big>(</big><em>format_string</em><big>)</big></dt>
<dd><p>Set the number format for a cell.</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>format_string</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 number format.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This method is used to define the numerical format of a number in Excel. It
controls whether a number is displayed as an integer, a floating point number,
a date, a currency value or some other user defined format.</p>
<p>The numerical format of a cell can be specified by using a format string or an
index to one of Excel’s built-in formats:</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">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="n">format1</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'d mmm yyyy'</span><span class="p">)</span> <span class="c"># Format string.</span>
<span class="n">format2</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="mh">0x0F</span><span class="p">)</span> <span class="c"># Format index.</span>
</pre></div>
</div>
<p>Format strings can control any aspect of number formatting allowed by Excel:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format01</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'0.000'</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">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">3.1415926</span><span class="p">,</span> <span class="n">format01</span><span class="p">)</span> <span class="c"># -> 3.142</span>
<span class="n">format02</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'#,##0'</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">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1234.56</span><span class="p">,</span> <span class="n">format02</span><span class="p">)</span> <span class="c"># -> 1,235</span>
<span class="n">format03</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'#,##0.00'</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">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1234.56</span><span class="p">,</span> <span class="n">format03</span><span class="p">)</span> <span class="c"># -> 1,234.56</span>
<span class="n">format04</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'0.00'</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">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">49.99</span><span class="p">,</span> <span class="n">format04</span><span class="p">)</span> <span class="c"># -> 49.99</span>
<span class="n">format05</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'mm/dd/yy'</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">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">36892.521</span><span class="p">,</span> <span class="n">format05</span><span class="p">)</span> <span class="c"># -> 01/01/01</span>
<span class="n">format06</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'mmm d yyyy'</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">6</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">36892.521</span><span class="p">,</span> <span class="n">format06</span><span class="p">)</span> <span class="c"># -> Jan 1 2001</span>
<span class="n">format07</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'d mmmm yyyy'</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">7</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">36892.521</span><span class="p">,</span> <span class="n">format07</span><span class="p">)</span> <span class="c"># -> 1 January 2001</span>
<span class="n">format08</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'dd/mm/yyyy hh:mm AM/PM'</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">8</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">36892.521</span><span class="p">,</span> <span class="n">format08</span><span class="p">)</span> <span class="c"># -> 01/01/2001 12:30 AM</span>
<span class="n">format09</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'0 "dollar and" .00 "cents"'</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">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1.87</span><span class="p">,</span> <span class="n">format09</span><span class="p">)</span> <span class="c"># -> 1 dollar and .87 cents</span>
<span class="c"># Conditional numerical formatting.</span>
<span class="n">format10</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'[Green]General;[Red]-General;General'</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">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="n">format10</span><span class="p">)</span> <span class="c"># > 0 Green</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">45</span><span class="p">,</span> <span class="n">format10</span><span class="p">)</span> <span class="c"># < 0 Red</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="mi">12</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="n">format10</span><span class="p">)</span> <span class="c"># = 0 Default colour</span>
<span class="c"># Zip code.</span>
<span class="n">format11</span><span class="o">.</span><span class="n">set_num_format</span><span class="p">(</span><span class="s">'00000'</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">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1209</span><span class="p">,</span> <span class="n">format11</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/formats_num_str.png" src="_images/formats_num_str.png" />
<p>The number system used for dates is described in
<a class="reference internal" href="working_with_dates_and_time.html#working-with-dates-and-time"><em>Working with Dates and Time</em></a>.</p>
<p>The colour format should have one of the following values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">Black</span><span class="p">]</span> <span class="p">[</span><span class="n">Blue</span><span class="p">]</span> <span class="p">[</span><span class="n">Cyan</span><span class="p">]</span> <span class="p">[</span><span class="n">Green</span><span class="p">]</span> <span class="p">[</span><span class="n">Magenta</span><span class="p">]</span> <span class="p">[</span><span class="n">Red</span><span class="p">]</span> <span class="p">[</span><span class="n">White</span><span class="p">]</span> <span class="p">[</span><span class="n">Yellow</span><span class="p">]</span>
</pre></div>
</div>
<p>For more information refer to the
<a class="reference external" href="http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx">Microsoft documentation on cell formats</a>.</p>
<p>Excel’s built-in formats are shown in the following table:</p>
<table border="1" class="docutils">
<colgroup>
<col width="10%" />
<col width="10%" />
<col width="80%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Index</th>
<th class="head">Index</th>
<th class="head">Format String</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>0x00</td>
<td><tt class="docutils literal"><span class="pre">General</span></tt></td>
</tr>
<tr class="row-odd"><td>1</td>
<td>0x01</td>
<td><tt class="docutils literal"><span class="pre">0</span></tt></td>
</tr>
<tr class="row-even"><td>2</td>
<td>0x02</td>
<td><tt class="docutils literal"><span class="pre">0.00</span></tt></td>
</tr>
<tr class="row-odd"><td>3</td>
<td>0x03</td>
<td><tt class="docutils literal"><span class="pre">#,##0</span></tt></td>
</tr>
<tr class="row-even"><td>4</td>
<td>0x04</td>
<td><tt class="docutils literal"><span class="pre">#,##0.00</span></tt></td>
</tr>
<tr class="row-odd"><td>5</td>
<td>0x05</td>
<td><tt class="docutils literal"><span class="pre">($#,##0_);($#,##0)</span></tt></td>
</tr>
<tr class="row-even"><td>6</td>
<td>0x06</td>
<td><tt class="docutils literal"><span class="pre">($#,##0_);[Red]($#,##0)</span></tt></td>
</tr>
<tr class="row-odd"><td>7</td>
<td>0x07</td>
<td><tt class="docutils literal"><span class="pre">($#,##0.00_);($#,##0.00)</span></tt></td>
</tr>
<tr class="row-even"><td>8</td>
<td>0x08</td>
<td><tt class="docutils literal"><span class="pre">($#,##0.00_);[Red]($#,##0.00)</span></tt></td>
</tr>
<tr class="row-odd"><td>9</td>
<td>0x09</td>
<td><tt class="docutils literal"><span class="pre">0%</span></tt></td>
</tr>
<tr class="row-even"><td>10</td>
<td>0x0a</td>
<td><tt class="docutils literal"><span class="pre">0.00%</span></tt></td>
</tr>
<tr class="row-odd"><td>11</td>
<td>0x0b</td>
<td><tt class="docutils literal"><span class="pre">0.00E+00</span></tt></td>
</tr>
<tr class="row-even"><td>12</td>
<td>0x0c</td>
<td><tt class="docutils literal"><span class="pre">#</span> <span class="pre">?/?</span></tt></td>
</tr>
<tr class="row-odd"><td>13</td>
<td>0x0d</td>
<td><tt class="docutils literal"><span class="pre">#</span> <span class="pre">??/??</span></tt></td>
</tr>
<tr class="row-even"><td>14</td>
<td>0x0e</td>
<td><tt class="docutils literal"><span class="pre">m/d/yy</span></tt></td>
</tr>
<tr class="row-odd"><td>15</td>
<td>0x0f</td>
<td><tt class="docutils literal"><span class="pre">d-mmm-yy</span></tt></td>
</tr>
<tr class="row-even"><td>16</td>
<td>0x10</td>
<td><tt class="docutils literal"><span class="pre">d-mmm</span></tt></td>
</tr>
<tr class="row-odd"><td>17</td>
<td>0x11</td>
<td><tt class="docutils literal"><span class="pre">mmm-yy</span></tt></td>
</tr>
<tr class="row-even"><td>18</td>
<td>0x12</td>
<td><tt class="docutils literal"><span class="pre">h:mm</span> <span class="pre">AM/PM</span></tt></td>
</tr>
<tr class="row-odd"><td>19</td>
<td>0x13</td>
<td><tt class="docutils literal"><span class="pre">h:mm:ss</span> <span class="pre">AM/PM</span></tt></td>
</tr>
<tr class="row-even"><td>20</td>
<td>0x14</td>
<td><tt class="docutils literal"><span class="pre">h:mm</span></tt></td>
</tr>
<tr class="row-odd"><td>21</td>
<td>0x15</td>
<td><tt class="docutils literal"><span class="pre">h:mm:ss</span></tt></td>
</tr>
<tr class="row-even"><td>22</td>
<td>0x16</td>
<td><tt class="docutils literal"><span class="pre">m/d/yy</span> <span class="pre">h:mm</span></tt></td>
</tr>
<tr class="row-odd"><td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr class="row-even"><td>37</td>
<td>0x25</td>
<td><tt class="docutils literal"><span class="pre">(#,##0_);(#,##0)</span></tt></td>
</tr>
<tr class="row-odd"><td>38</td>
<td>0x26</td>
<td><tt class="docutils literal"><span class="pre">(#,##0_);[Red](#,##0)</span></tt></td>
</tr>
<tr class="row-even"><td>39</td>
<td>0x27</td>
<td><tt class="docutils literal"><span class="pre">(#,##0.00_);(#,##0.00)</span></tt></td>
</tr>
<tr class="row-odd"><td>40</td>
<td>0x28</td>
<td><tt class="docutils literal"><span class="pre">(#,##0.00_);[Red](#,##0.00)</span></tt></td>
</tr>
<tr class="row-even"><td>41</td>
<td>0x29</td>
<td><tt class="docutils literal"><span class="pre">_(*</span> <span class="pre">#,##0_);_(*</span> <span class="pre">(#,##0);_(*</span> <span class="pre">"-"_);_(@_)</span></tt></td>
</tr>
<tr class="row-odd"><td>42</td>
<td>0x2a</td>
<td><tt class="docutils literal"><span class="pre">_($*</span> <span class="pre">#,##0_);_($*</span> <span class="pre">(#,##0);_($*</span> <span class="pre">"-"_);_(@_)</span></tt></td>
</tr>
<tr class="row-even"><td>43</td>
<td>0x2b</td>
<td><tt class="docutils literal"><span class="pre">_(*</span> <span class="pre">#,##0.00_);_(*</span> <span class="pre">(#,##0.00);_(*</span> <span class="pre">"-"??_);_(@_)</span></tt></td>
</tr>
<tr class="row-odd"><td>44</td>
<td>0x2c</td>
<td><tt class="docutils literal"><span class="pre">_($*</span> <span class="pre">#,##0.00_);_($*</span> <span class="pre">(#,##0.00);_($*</span> <span class="pre">"-"??_);_(@_)</span></tt></td>
</tr>
<tr class="row-even"><td>45</td>
<td>0x2d</td>
<td><tt class="docutils literal"><span class="pre">mm:ss</span></tt></td>
</tr>
<tr class="row-odd"><td>46</td>
<td>0x2e</td>
<td><tt class="docutils literal"><span class="pre">[h]:mm:ss</span></tt></td>
</tr>
<tr class="row-even"><td>47</td>
<td>0x2f</td>
<td><tt class="docutils literal"><span class="pre">mm:ss.0</span></tt></td>
</tr>
<tr class="row-odd"><td>48</td>
<td>0x30</td>
<td><tt class="docutils literal"><span class="pre">##0.0E+0</span></tt></td>
</tr>
<tr class="row-even"><td>49</td>
<td>0x31</td>
<td><tt class="docutils literal"><span class="pre">@</span></tt></td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Numeric formats 23 to 36 are not documented by Microsoft and
may differ in international versions.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The dollar sign appears as the defined local currency symbol.</p>
</div>
</div>
<div class="section" id="format-set-locked">
<h2>format.set_locked()</h2>
<dl class="function">
<dt id="set_locked">
<tt class="descname">set_locked</tt><big>(</big><em>state</em><big>)</big></dt>
<dd><p>Set the cell locked state.</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>state</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#bool" title="(in Python v2.7)"><em>bool</em></a>) – Turn cell locking on or off. Defaults to True.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This property can be used to prevent modification of a cells contents.
Following Excel’s convention, cell locking is turned on by default. However,
it only has an effect if the worksheet has been protected, see the worksheet
<tt class="docutils literal"><span class="pre">protect()</span></tt> method (not implemented yet):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">locked</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">locked</span><span class="o">.</span><span class="n">set_locked</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="n">unlocked</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">locked</span><span class="o">.</span><span class="n">set_locked</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="c"># Enable worksheet protection</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">protect</span><span class="p">()</span>
<span class="c"># This cell cannot be edited.</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">'=1+2'</span><span class="p">,</span> <span class="n">locked</span><span class="p">)</span>
<span class="c"># This cell can be edited.</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'A2'</span><span class="p">,</span> <span class="s">'=1+2'</span><span class="p">,</span> <span class="n">unlocked</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="format-set-hidden">
<h2>format.set_hidden()</h2>
<dl class="function">
<dt id="set_hidden">
<tt class="descname">set_hidden</tt><big>(</big><big>)</big></dt>
<dd><p>Hide formulas in a cell.</p>
</dd></dl>
<p>This property is used to hide a formula while still displaying its result. This
is generally used to hide complex calculations from end users who are only
interested in the result. It only has an effect if the worksheet has been
protected, see the worksheet <tt class="docutils literal"><span class="pre">protect()</span></tt> method (not implemented yet):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hidden</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">hidden</span><span class="o">.</span><span class="n">set_hidden</span><span class="p">()</span>
<span class="c"># Enable worksheet protection</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">protect</span><span class="p">()</span>
<span class="c"># The formula in this cell isn't visible</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">'=1+2'</span><span class="p">,</span> <span class="n">hidden</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="format-set-align">
<h2>format.set_align()</h2>
<dl class="function">
<dt id="set_align">
<tt class="descname">set_align</tt><big>(</big><em>alignment</em><big>)</big></dt>
<dd><p>Set the alignment for data in the cell.</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>alignment</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 vertical and or horizontal alignment direction.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This method is used to set the horizontal and vertical text alignment within a
cell. The following are the available horizontal alignments:</p>
<table border="1" class="docutils">
<colgroup>
<col width="100%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Horizontal alignment</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>center</td>
</tr>
<tr class="row-odd"><td>right</td>
</tr>
<tr class="row-even"><td>fill</td>
</tr>
<tr class="row-odd"><td>justify</td>
</tr>
<tr class="row-even"><td>center_across</td>
</tr>
</tbody>
</table>
<p>The following are the available vertical alignments:</p>
<table border="1" class="docutils">
<colgroup>
<col width="100%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Vertical alignment</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>top</td>
</tr>
<tr class="row-odd"><td>vcenter</td>
</tr>
<tr class="row-even"><td>bottom</td>
</tr>
<tr class="row-odd"><td>vjustify</td>
</tr>
</tbody>
</table>
<p>As in Excel, vertical and horizontal alignments can be combined:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_align</span><span class="p">(</span><span class="s">'center'</span><span class="p">)</span>
<span class="n">format</span><span class="o">.</span><span class="n">set_align</span><span class="p">(</span><span class="s">'vcenter'</span><span class="p">)</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">set_row</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">30</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">'Some Text'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>Text can be aligned across two or more adjacent cells using the
<tt class="docutils literal"><span class="pre">'center_across'</span></tt> property. However, for genuine merged cells it is better
to use the <tt class="docutils literal"><span class="pre">merge_range()</span></tt> worksheet method (not implemented yet).</p>
<p>The <tt class="docutils literal"><span class="pre">'vjustify'</span></tt> (vertical justify) option can be used to provide automatic
text wrapping in a cell. The height of the cell will be adjusted to
accommodate the wrapped text. To specify where the text wraps use the
<tt class="docutils literal"><span class="pre">set_text_wrap()</span></tt> method.</p>
</div>
<div class="section" id="format-set-center-across">
<h2>format.set_center_across()</h2>
<dl class="function">
<dt id="set_center_across">
<tt class="descname">set_center_across</tt><big>(</big><big>)</big></dt>
<dd><p>Centre text across adjacent cells.</p>
</dd></dl>
<p>Text can be aligned across two or more adjacent cells using the
<tt class="docutils literal"><span class="pre">set_center_across()</span></tt> method. This is an alias for the
<tt class="docutils literal"><span class="pre">set_align('center_across')</span></tt> method call.</p>
<p>Only one cell should contain the text, the other cells should be blank:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_center_across</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">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'Center across selection'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
<span class="n">worksheet</span><span class="o">.</span><span class="n">write_blank</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>For actual merged cells it is better to use the <tt class="docutils literal"><span class="pre">merge_range()</span></tt> worksheet
method.</p>
</div>
<div class="section" id="format-set-text-wrap">
<h2>format.set_text_wrap()</h2>
<dl class="function">
<dt id="set_text_wrap">
<tt class="descname">set_text_wrap</tt><big>(</big><big>)</big></dt>
<dd><p>Wrap text in a cell.</p>
</dd></dl>
<p>Turn text wrapping on for text in a cell:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_text_wrap</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">"Some long text to wrap in a cell"</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>If you wish to control where the text is wrapped you can add newline characters
to the string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_text_wrap</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">"It's</span><span class="se">\n</span><span class="s">a bum</span><span class="se">\n</span><span class="s">wrap"</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>Excel will adjust the height of the row to accommodate the wrapped text. A
similar effect can be obtained without newlines using the
<tt class="docutils literal"><span class="pre">set_align('vjustify')</span></tt> method.</p>
</div>
<div class="section" id="format-set-rotation">
<h2>format.set_rotation()</h2>
<dl class="function">
<dt id="set_rotation">
<tt class="descname">set_rotation</tt><big>(</big><em>angle</em><big>)</big></dt>
<dd><p>Set the rotation of the text in a cell.</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>angle</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Rotation angle in the range -90 to 90 and 270.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the rotation of the text in a cell. The rotation can be any angle in the
range -90 to 90 degrees:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_rotation</span><span class="p">(</span><span class="mi">30</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">'This text is rotated'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>The angle 270 is also supported. This indicates text where the letters run from
top to bottom.</p>
</div>
<div class="section" id="format-set-indent">
<h2>format.set_indent()</h2>
<dl class="function">
<dt id="set_indent">
<tt class="descname">set_indent</tt><big>(</big><em>level</em><big>)</big></dt>
<dd><p>Set the cell text indentation level.</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>level</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Indentation level.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>This method can be used to indent text in a cell. The argument, which should be
an integer, is taken as the level of indentation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_indent</span><span class="p">(</span><span class="mi">2</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">'This text is indented'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<p>Indentation is a horizontal alignment property. It will override any other
horizontal properties but it can be used in conjunction with vertical
properties.</p>
</div>
<div class="section" id="format-set-shrink">
<h2>format.set_shrink()</h2>
<dl class="function">
<dt id="set_shrink">
<tt class="descname">set_shrink</tt><big>(</big><big>)</big></dt>
<dd><p>Turn on the text “shrink to fit” for a cell.</p>
</dd></dl>
<p>This method can be used to shrink text so that it fits in a cell:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_shrink</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">'Honey, I shrunk the text!'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="format-set-text-justlast">
<h2>format.set_text_justlast()</h2>
<dl class="function">
<dt id="set_text_justlast">
<tt class="descname">set_text_justlast</tt><big>(</big><big>)</big></dt>
<dd><p>Turn on the justify last text property.</p>
</dd></dl>
<p>Only applies to Far Eastern versions of Excel.</p>
</div>
<div class="section" id="format-set-pattern">
<h2>format.set_pattern()</h2>
<dl class="function">
<dt id="set_pattern">
<tt class="descname">set_pattern</tt><big>(</big><em>index</em><big>)</big></dt>
<dd><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>index</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Pattern index. 0 - 18.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the background pattern of a cell.</p>
<p>The most common pattern is 1 which is a solid fill of the background color.</p>
</div>
<div class="section" id="format-set-bg-color">
<h2>format.set_bg_color()</h2>
<dl class="function">
<dt id="set_bg_color">
<tt class="descname">set_bg_color</tt><big>(</big><em>color</em><big>)</big></dt>
<dd><p>Set the color of the background pattern in a cell.</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>color</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 font color.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">set_bg_color()</span></tt> method can be used to set the background colour of a
pattern. Patterns are defined via the <tt class="docutils literal"><span class="pre">set_pattern()</span></tt> method. If a pattern
hasn’t been defined then a solid fill pattern is used as the default.</p>
<p>Here is an example of how to set up a solid fill in a cell:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">format</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">format</span><span class="o">.</span><span class="n">set_pattern</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c"># This is optional when using a solid fill.</span>
<span class="n">format</span><span class="o">.</span><span class="n">set_bg_color</span><span class="p">(</span><span class="s">'green'</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">'Ray'</span><span class="p">,</span> <span class="n">format</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/formats_set_bg_color.png" src="_images/formats_set_bg_color.png" />
<p>The color can be a Html style <tt class="docutils literal"><span class="pre">#RRGGBB</span></tt> string or a limited number of named
colors, see <a class="reference internal" href="working_with_formats.html#format-colors"><em>Format Colors</em></a>.</p>
</div>
<div class="section" id="format-set-fg-color">
<h2>format.set_fg_color()</h2>
<dl class="function">
<dt id="set_fg_color">
<tt class="descname">set_fg_color</tt><big>(</big><em>color</em><big>)</big></dt>
<dd><p>Set the color of the foreground pattern in a cell.</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>color</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 font color.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <tt class="docutils literal"><span class="pre">set_fg_color()</span></tt> method can be used to set the foreground colour of a
pattern.</p>
<p>The color can be a Html style <tt class="docutils literal"><span class="pre">#RRGGBB</span></tt> string or a limited number of named
colors, see <a class="reference internal" href="working_with_formats.html#format-colors"><em>Format Colors</em></a>.</p>
</div>
<div class="section" id="format-set-border">
<h2>format.set_border()</h2>
<dl class="function">
<dt id="set_border">
<tt class="descname">set_border</tt><big>(</big><em>style</em><big>)</big></dt>
<dd><p>Set the cell border style.</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>style</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Border style index. Default is 1.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Individual border elements can be configured using the following methods with
the same parameters:</p>
<ul class="simple">
<li><a class="reference internal" href="#set_bottom" title="set_bottom"><tt class="xref py py-func docutils literal"><span class="pre">set_bottom()</span></tt></a></li>
<li><a class="reference internal" href="#set_top" title="set_top"><tt class="xref py py-func docutils literal"><span class="pre">set_top()</span></tt></a></li>
<li><a class="reference internal" href="#set_left" title="set_left"><tt class="xref py py-func docutils literal"><span class="pre">set_left()</span></tt></a></li>
<li><a class="reference internal" href="#set_right" title="set_right"><tt class="xref py py-func docutils literal"><span class="pre">set_right()</span></tt></a></li>
</ul>
<p>A cell border is comprised of a border on the bottom, top, left and right.
These can be set to the same value using <tt class="docutils literal"><span class="pre">set_border()</span></tt> or individually
using the relevant method calls shown above.</p>
<p>The following shows the border styles sorted by XlsxWriter index number:</p>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="32%" />
<col width="17%" />
<col width="36%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Index</th>
<th class="head">Name</th>
<th class="head">Weight</th>
<th class="head">Style</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>None</td>
<td>0</td>
<td> </td>
</tr>
<tr class="row-odd"><td>1</td>
<td>Continuous</td>
<td>1</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-even"><td>2</td>
<td>Continuous</td>
<td>2</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-odd"><td>3</td>
<td>Dash</td>
<td>1</td>
<td><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></tt></td>
</tr>
<tr class="row-even"><td>4</td>
<td>Dot</td>
<td>1</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>5</td>
<td>Continuous</td>
<td>3</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-even"><td>6</td>
<td>Double</td>
<td>3</td>
<td><tt class="docutils literal"><span class="pre">===========</span></tt></td>
</tr>
<tr class="row-odd"><td>7</td>
<td>Continuous</td>
<td>0</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-even"><td>8</td>
<td>Dash</td>
<td>2</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>9</td>
<td>Dash Dot</td>
<td>1</td>
<td><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></tt></td>
</tr>
<tr class="row-even"><td>10</td>
<td>Dash Dot</td>
<td>2</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>11</td>
<td>Dash Dot Dot</td>
<td>1</td>
<td><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></tt></td>
</tr>
<tr class="row-even"><td>12</td>
<td>Dash Dot Dot</td>
<td>2</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>13</td>
<td>SlantDash Dot</td>
<td>2</td>
<td><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></tt></td>
</tr>
</tbody>
</table>
<p>The following shows the borders in the order shown in the Excel Dialog:</p>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="35%" />
<col width="15%" />
<col width="35%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Index</th>
<th class="head">Style</th>
<th class="head">Index</th>
<th class="head">Style</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>None</td>
<td>12</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>7</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
<td>13</td>
<td><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></tt></td>
</tr>
<tr class="row-even"><td>4</td>
<td><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></tt></td>
<td>10</td>
<td><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></tt></td>
</tr>
<tr class="row-odd"><td>11</td>
<td><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></tt></td>
<td>8</td>
<td><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></tt></td>
</tr>
<tr class="row-even"><td>9</td>
<td><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></tt></td>
<td>2</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-odd"><td>3</td>
<td><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></tt></td>
<td>5</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
</tr>
<tr class="row-even"><td>1</td>
<td><tt class="docutils literal"><span class="pre">-----------</span></tt></td>
<td>6</td>
<td><tt class="docutils literal"><span class="pre">===========</span></tt></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="format-set-bottom">
<h2>format.set_bottom()</h2>
<dl class="function">
<dt id="set_bottom">
<tt class="descname">set_bottom</tt><big>(</big><em>style</em><big>)</big></dt>
<dd><p>Set the cell bottom border style.</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>style</strong> (<a class="reference external" href="http://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a>) – Border style index. Default is 1.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Set the cell bottom border style. See <a class="reference internal" href="#set_border" title="set_border"><tt class="xref py py-func docutils literal"><span class="pre">set_border()</span></tt></a> for details on the
border styles.</p>
</div>
<div class="section" id="format-set-top">
<h2>format.set_top()</h2>
<dl class="function">
<dt id="set_top">
<tt class="descname">set_top</tt><big>(</big><em>style</em><big>)</big></dt>
<dd><p>Set the cell top border style.</p>
<table class="docutils field-list" frame="void" rules="none">