XlsxWriter

raw JSON →
3.2.9 verified Tue May 12 auth: no python install: verified quickstart: verified

XlsxWriter is a Python module for creating Excel XLSX files. It supports Python 3.8+ and PyPy3 and uses standard libraries only. It offers comprehensive features for writing, formatting, and customizing Excel spreadsheets, including charts, formulas, data validation, and images. It is actively maintained with regular releases.

pip install XlsxWriter
error ModuleNotFoundError: No module named 'xlsxwriter'
cause The XlsxWriter library is not installed in your Python environment.
fix
pip install XlsxWriter
error AttributeError: 'Workbook' object has no attribute 'add_worksheet'
cause You are calling a misspelled or non-existent method on the Workbook object; the correct method name is add_worksheet().
fix
worksheet = workbook.add_worksheet('Sheet1')
error TypeError: write() missing 2 required positional arguments: 'col' and 'token'
cause The write() method requires at least three positional arguments: the row, the column, and the value to write.
fix
worksheet.write(0, 0, 'Hello World')
error ValueError: Invalid argument to merge_range(): 'first_row' must be <= 'last_row' and 'first_col' must be <= 'last_col'.
cause The merge_range() method was called with invalid coordinates where the starting row or column is greater than the ending row or column.
fix
worksheet.merge_range(0, 0, 0, 2, 'Merged Cells')
breaking XlsxWriter dropped support for Python 2 after release 2.0. Versions 3.0 and later are exclusively for Python 3.8+.
fix Ensure you are using Python 3.8 or newer for XlsxWriter versions > 2.0.
gotcha XlsxWriter is primarily for *creating* new XLSX files. It cannot read or modify existing Excel files directly. For modifying existing files, users often need to read with a different library (e.g., openpyxl) and then use XlsxWriter to write new content or use it as an engine with pandas.ExcelWriter for appending data.
fix If modification of existing files is required, consider using `openpyxl` for read/write operations or combine `pandas.ExcelWriter` with `openpyxl` as the engine for appending.
gotcha Failing to call `workbook.close()` will result in an unsaved or corrupted Excel file.
fix Always ensure `workbook.close()` is called after all write operations are complete, typically as the last step in your script. Using a `with` statement with `pandas.ExcelWriter` (which uses XlsxWriter as an engine) handles this automatically.
gotcha XlsxWriter uses zero-indexed rows and columns (e.g., `(0, 0)` for 'A1'), which can be confusing for users accustomed to 1-indexed Excel notation or A1-style strings. However, `worksheet.write()` also accepts A1-style notation.
fix Be mindful of zero-indexing when using `(row, col)` coordinates. If preferred, use A1-style cell notation (e.g., `'A1'`) with methods like `write()`.
gotcha When integrating with Pandas, `XlsxWriter` is typically specified as the engine for `pd.ExcelWriter(..., engine='xlsxwriter')`. In this scenario, you usually don't need to explicitly import `xlsxwriter` at the top of your script for basic dataframe output.
fix Understand that `pandas.ExcelWriter` handles the interaction with XlsxWriter when `engine='xlsxwriter'` is set. Direct `xlsxwriter` imports are primarily for standalone use or for advanced customizations that require direct access to XlsxWriter objects (like `workbook = writer.book`).
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.09s 19.2M
3.10 alpine (musl) - - 0.09s 19.2M
3.10 slim (glibc) wheel 1.6s 0.06s 20M
3.10 slim (glibc) - - 0.06s 20M
3.11 alpine (musl) wheel - 0.16s 21.4M
3.11 alpine (musl) - - 0.18s 21.4M
3.11 slim (glibc) wheel 1.7s 0.13s 22M
3.11 slim (glibc) - - 0.13s 22M
3.12 alpine (musl) wheel - 0.12s 13.2M
3.12 alpine (musl) - - 0.13s 13.2M
3.12 slim (glibc) wheel 1.6s 0.14s 14M
3.12 slim (glibc) - - 0.13s 14M
3.13 alpine (musl) wheel - 0.11s 13.0M
3.13 alpine (musl) - - 0.12s 12.9M
3.13 slim (glibc) wheel 1.6s 0.12s 13M
3.13 slim (glibc) - - 0.12s 13M
3.9 alpine (musl) wheel - 0.08s 18.7M
3.9 alpine (musl) - - 0.09s 18.7M
3.9 slim (glibc) wheel 1.9s 0.08s 19M
3.9 slim (glibc) - - 0.09s 19M

This quickstart creates a new Excel file named `hello.xlsx`, adds a single worksheet, writes 'Hello XlsxWriter!' to cell A1, and saves the file.

import xlsxwriter

# Create a new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

# Write a simple string to cell A1.
worksheet.write('A1', 'Hello XlsxWriter!')

# Close the workbook to save the file.
workbook.close()
print("Created hello.xlsx")