A Python module for creating Excel XLSX files.
npx @tessl/cli install tessl/pypi-xlsxwriter@3.2.0A 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.
pip install xlsxwriterimport xlsxwriterMost common pattern:
import xlsxwriter
# Create workbook and worksheet
workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()import xlsxwriter
# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
# Write some data with formatting
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'Hello', bold)
worksheet.write('B1', 'World')
worksheet.write('A2', 123)
worksheet.write('B2', 456.789)
# Add a formula
worksheet.write('A3', '=SUM(A2:B2)')
# Insert a chart
chart = workbook.add_chart({'type': 'column'})
chart.add_series({
'categories': '=Sheet1!A1:B1',
'values': '=Sheet1!A2:B2',
})
worksheet.insert_chart('D2', chart)
# Close the workbook
workbook.close()XlsxWriter uses a hierarchical structure for building Excel files:
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.
Core workbook functionality for creating, configuring, and managing Excel files including worksheet creation, format definition, chart creation, and global workbook settings.
class Workbook:
def __init__(self, filename, options=None): ...
def add_worksheet(self, name=None, worksheet_class=None): ...
def add_format(self, properties=None): ...
def add_chart(self, options): ...
def close(self): ...
def set_properties(self, properties): ...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.
def write(row, col, *args): ...
def write_string(row, col, string, cell_format=None): ...
def write_number(row, col, number, cell_format=None): ...
def write_formula(row, col, formula, cell_format=None, value=0): ...
def write_datetime(row, col, date, cell_format=None): ...
def write_url(row, col, url, cell_format=None, string=None, tip=None): ...Complete cell formatting system including fonts, colors, borders, alignment, number formats, patterns, and fill options with support for conditional formatting and cell protection.
class Format:
def set_font_name(self, font_name): ...
def set_font_size(self, font_size=11): ...
def set_bold(self, bold=True): ...
def set_align(self, alignment): ...
def set_bg_color(self, bg_color): ...
def set_border(self, style=1): ...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.
class Chart:
def add_series(self, options=None): ...
def set_title(self, options=None): ...
def set_x_axis(self, options): ...
def set_y_axis(self, options): ...
def set_legend(self, options): ...Advanced Excel functionality including data validation, conditional formatting, autofilters, tables, sparklines, images, textboxes, comments, and worksheet protection.
def data_validation(first_row, first_col, last_row, last_col, options=None): ...
def conditional_format(first_row, first_col, last_row, last_col, options=None): ...
def add_table(first_row, first_col, last_row, last_col, options=None): ...
def insert_image(row, col, source, options=None): ...
def autofilter(first_row, first_col, last_row, last_col): ...Complete page setup and print configuration including orientation, margins, headers/footers, page breaks, print areas, scaling options, and paper size settings.
def set_landscape(): ...
def set_margins(left=0.7, right=0.7, top=0.75, bottom=0.75): ...
def set_header(header="", options=None, margin=None): ...
def print_area(first_row, first_col, last_row, last_col): ...
def fit_to_pages(width, height): ...Essential utility functions for Excel coordinate conversions, cell reference handling, range creation, and worksheet name management.
def xl_rowcol_to_cell(row, col, row_abs=False, col_abs=False): ...
def xl_cell_to_rowcol(cell_str): ...
def xl_range(first_row, first_col, last_row, last_col): ...
def quote_sheetname(sheetname): ...class XlsxWriterException(Exception):
"""Base exception for XlsxWriter."""
class XlsxInputError(XlsxWriterException):
"""Base exception for all input data related errors."""
class XlsxFileError(XlsxWriterException):
"""Base exception for all file related errors."""