XlsxWriter
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.
Warnings
- breaking XlsxWriter dropped support for Python 2 after release 2.0. Versions 3.0 and later are exclusively for Python 3.8+.
- 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.
- gotcha Failing to call `workbook.close()` will result in an unsaved or corrupted Excel file.
- 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.
- 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.
Install
-
pip install XlsxWriter
Imports
- xlsxwriter
import xlsxwriter
Quickstart
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")