0
# XlsxWriter
1
2
A comprehensive Python library for creating Excel 2007+ XLSX files with full formatting capabilities. XlsxWriter provides extensive functionality for writing text, numbers, formulas, and hyperlinks to multiple worksheets with support for advanced Excel features including charts, merged cells, conditional formatting, data validation, autofilters, images, and macros.
3
4
## Package Information
5
6
- **Package Name**: xlsxwriter
7
- **Language**: Python
8
- **Installation**: `pip install xlsxwriter`
9
- **Python Version**: 3.8+ and PyPy3
10
- **Dependencies**: Standard library only
11
12
## Core Imports
13
14
```python
15
import xlsxwriter
16
```
17
18
Most common pattern:
19
20
```python
21
import xlsxwriter
22
23
# Create workbook and worksheet
24
workbook = xlsxwriter.Workbook('output.xlsx')
25
worksheet = workbook.add_worksheet()
26
```
27
28
## Basic Usage
29
30
```python
31
import xlsxwriter
32
33
# Create a workbook and add a worksheet
34
workbook = xlsxwriter.Workbook('demo.xlsx')
35
worksheet = workbook.add_worksheet()
36
37
# Write some data with formatting
38
bold = workbook.add_format({'bold': True})
39
worksheet.write('A1', 'Hello', bold)
40
worksheet.write('B1', 'World')
41
worksheet.write('A2', 123)
42
worksheet.write('B2', 456.789)
43
44
# Add a formula
45
worksheet.write('A3', '=SUM(A2:B2)')
46
47
# Insert a chart
48
chart = workbook.add_chart({'type': 'column'})
49
chart.add_series({
50
'categories': '=Sheet1!A1:B1',
51
'values': '=Sheet1!A2:B2',
52
})
53
worksheet.insert_chart('D2', chart)
54
55
# Close the workbook
56
workbook.close()
57
```
58
59
## Architecture
60
61
XlsxWriter uses a hierarchical structure for building Excel files:
62
63
- **Workbook**: Top-level container managing worksheets, formats, and global settings
64
- **Worksheet**: Individual sheet containing data, formatting, and objects like charts/images
65
- **Format**: Styling objects for fonts, colors, borders, alignment, and number formatting
66
- **Chart**: Visualization objects with various types (column, line, pie, etc.)
67
- **Utilities**: Helper functions for Excel coordinate conversions and reference handling
68
69
The library provides 100% compatibility with Excel XLSX format and includes memory optimization for large files, integration with Pandas/Polars, and support for advanced Excel features like macros and VBA projects.
70
71
## Capabilities
72
73
### Workbook Management
74
75
Core workbook functionality for creating, configuring, and managing Excel files including worksheet creation, format definition, chart creation, and global workbook settings.
76
77
```python { .api }
78
class Workbook:
79
def __init__(self, filename, options=None): ...
80
def add_worksheet(self, name=None, worksheet_class=None): ...
81
def add_format(self, properties=None): ...
82
def add_chart(self, options): ...
83
def close(self): ...
84
def set_properties(self, properties): ...
85
```
86
87
[Workbook Management](./workbook.md)
88
89
### Data Writing and Cell Operations
90
91
Comprehensive data writing capabilities including support for all Excel data types, formulas, hyperlinks, rich text, and specialized writing methods for rows, columns, and cell ranges.
92
93
```python { .api }
94
def write(row, col, *args): ...
95
def write_string(row, col, string, cell_format=None): ...
96
def write_number(row, col, number, cell_format=None): ...
97
def write_formula(row, col, formula, cell_format=None, value=0): ...
98
def write_datetime(row, col, date, cell_format=None): ...
99
def write_url(row, col, url, cell_format=None, string=None, tip=None): ...
100
```
101
102
[Data Writing](./data-writing.md)
103
104
### Formatting and Styling
105
106
Complete cell formatting system including fonts, colors, borders, alignment, number formats, patterns, and fill options with support for conditional formatting and cell protection.
107
108
```python { .api }
109
class Format:
110
def set_font_name(self, font_name): ...
111
def set_font_size(self, font_size=11): ...
112
def set_bold(self, bold=True): ...
113
def set_align(self, alignment): ...
114
def set_bg_color(self, bg_color): ...
115
def set_border(self, style=1): ...
116
```
117
118
[Formatting and Styling](./formatting.md)
119
120
### Charts and Visualization
121
122
Comprehensive charting system supporting all major Excel chart types including column, line, pie, scatter, area, bar, radar, doughnut, and stock charts with full customization options.
123
124
```python { .api }
125
class Chart:
126
def add_series(self, options=None): ...
127
def set_title(self, options=None): ...
128
def set_x_axis(self, options): ...
129
def set_y_axis(self, options): ...
130
def set_legend(self, options): ...
131
```
132
133
[Charts and Visualization](./charts.md)
134
135
### Advanced Features
136
137
Advanced Excel functionality including data validation, conditional formatting, autofilters, tables, sparklines, images, textboxes, comments, and worksheet protection.
138
139
```python { .api }
140
def data_validation(first_row, first_col, last_row, last_col, options=None): ...
141
def conditional_format(first_row, first_col, last_row, last_col, options=None): ...
142
def add_table(first_row, first_col, last_row, last_col, options=None): ...
143
def insert_image(row, col, source, options=None): ...
144
def autofilter(first_row, first_col, last_row, last_col): ...
145
```
146
147
[Advanced Features](./advanced-features.md)
148
149
### Page Setup and Printing
150
151
Complete page setup and print configuration including orientation, margins, headers/footers, page breaks, print areas, scaling options, and paper size settings.
152
153
```python { .api }
154
def set_landscape(): ...
155
def set_margins(left=0.7, right=0.7, top=0.75, bottom=0.75): ...
156
def set_header(header="", options=None, margin=None): ...
157
def print_area(first_row, first_col, last_row, last_col): ...
158
def fit_to_pages(width, height): ...
159
```
160
161
[Page Setup and Printing](./page-setup.md)
162
163
### Utility Functions and Helpers
164
165
Essential utility functions for Excel coordinate conversions, cell reference handling, range creation, and worksheet name management.
166
167
```python { .api }
168
def xl_rowcol_to_cell(row, col, row_abs=False, col_abs=False): ...
169
def xl_cell_to_rowcol(cell_str): ...
170
def xl_range(first_row, first_col, last_row, last_col): ...
171
def quote_sheetname(sheetname): ...
172
```
173
174
[Utility Functions](./utilities.md)
175
176
## Types
177
178
```python { .api }
179
class XlsxWriterException(Exception):
180
"""Base exception for XlsxWriter."""
181
182
class XlsxInputError(XlsxWriterException):
183
"""Base exception for all input data related errors."""
184
185
class XlsxFileError(XlsxWriterException):
186
"""Base exception for all file related errors."""
187
```