import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook("demo.xlsx")
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column("A:A", 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({"bold": True})
# Write some simple text.
worksheet.write("A1", "Hello")
# Text with formatting.
worksheet.write("A2", "World", bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()
print(f"Saved demo.xlsx with worksheet {worksheet.name!r}")XlsxWriter writes Excel .xlsx files from Python. It handles cell formatting, formulas, charts, conditional formatting, data validation and images, and it produces files that open cleanly in Excel, Google Sheets and LibreOffice. It only writes. To read or change a workbook that already exists, use openpyxl instead. This page is an online XlsxWriter compiler: the code runs in your browser, so you can try it without installing anything. Run the example first, then paste any snippet below into a new cell to try it.
xlsxwriter.Workbook("demo.xlsx") starts a workbook and
add_worksheet() adds a sheet named "Sheet1". set_column("A:A", 20)
widens column A. add_format({"bold": True}) creates a format that any
write() call can use. Cells can be addressed as "A1" or as
(row, column), where both numbers start at zero, so write(2, 0, 123)
fills A3. Nothing is written to disk until workbook.close().
import xlsxwriter
with xlsxwriter.Workbook("totals.xlsx") as workbook:
worksheet = workbook.add_worksheet()
worksheet.write_column("A1", [120, 135, 150])
worksheet.write_formula("A4", "=SUM(A1:A3)")
Using with closes the workbook for you. XlsxWriter does not calculate
formulas: Excel does that when the file is opened. A program that reads the
file without Excel sees 0 in A4 unless you pass the result yourself, as in
write_formula("A4", "=SUM(A1:A3)", None, 405).
Charts are native Excel charts, so they stay editable:
import xlsxwriter
with xlsxwriter.Workbook("chart.xlsx") as workbook:
worksheet = workbook.add_worksheet("Sales")
worksheet.write_row("A1", ["Quarter", "Sales"])
worksheet.write_column("A2", ["Q1", "Q2", "Q3"])
worksheet.write_column("B2", [120, 135, 150])
chart = workbook.add_chart({"type": "column"})
chart.add_series({
"name": "Sales",
"categories": "=Sales!$A$2:$A$4",
"values": "=Sales!$B$2:$B$4",
})
worksheet.insert_chart("D2", chart)
pandas can use XlsxWriter as its Excel engine, and you can still reach the worksheet to format it:
import pandas as pd
df = pd.DataFrame({"Quarter": ["Q1", "Q2", "Q3"], "Sales": [120, 135, 150]})
with pd.ExcelWriter("report.xlsx", engine="xlsxwriter") as writer:
df.to_excel(writer, sheet_name="Sales", index=False)
writer.sheets["Sales"].set_column("A:B", 14)
Conditional formats are kept as rules, so Excel updates the colours when the numbers change:
import xlsxwriter
with xlsxwriter.Workbook("scores.xlsx") as workbook:
worksheet = workbook.add_worksheet()
worksheet.write_column("A1", [45, 72, 88, 91, 60])
worksheet.conditional_format("A1:A5", {"type": "3_color_scale"})
Files your code saves, like demo.xlsx, appear in the sidebar's Data
tab. Running code and inspecting the result is free. Downloading the
files requires a Premium plan.
xlsxwriter.Workbook("demo.xlsx")
on a file that exists starts an empty workbook, and closing it replaces
the file.[ ] : * ? / \. XlsxWriter raises InvalidWorksheetName otherwise.xlsxwriter.Workbook("big.xlsx", {"constant_memory": True}) writes each row as soon as the next begins,
which keeps memory flat. Rows then have to be written in order.