Back to Blog
Python

Python XlsxWriter: Create Excel and Write Pandas DataFrame

python xlsxwriter create excel and write pandas dataframe: Learn how to use XlsxWriter with pandas to create Excel files, write DataFrames, and control formatting, dat...

XlsxWriterpandasExcelDataFramedata export
Illustration of a pandas DataFrame being written into an Excel spreadsheet using XlsxWriter, with a gear icon representing the engine.

When you need to generate an Excel file from a pandas DataFrame, XlsxWriter is one of the most flexible engines available. The combination python xlsxwriter create excel and write pandas dataframe describes a common workflow: using pandas' to_excel method with the XlsxWriter engine to gain control over the output file beyond what the default engine offers.

The Core Pattern: to_excel with engine='xlsxwriter'

The simplest way to write a DataFrame to an Excel file is to call to_excel directly. By specifying engine='xlsxwriter', you tell pandas to use XlsxWriter as the backend:

import pandas as pd df = pd.DataFrame({ "Product": ["Widget", "Gadget", "Sprocket"], "Price": [25.0, 35.5, 12.75], "Quantity": [10, 4, 8] }) df.to_excel("output.xlsx", engine="xlsxwriter", index=False)

This creates a workbook with a single worksheet named Sheet1 containing the DataFrame data. The index=False argument prevents pandas from writing the row index as a separate column. Without it, you get an extra Unnamed: 0 column with integer labels.

This pattern is sufficient when you only need a plain export. The real value of XlsxWriter appears when you need formatting, multiple sheets, or precise control over how data is written.

Installing the Required Packages

You need both pandas and XlsxWriter installed. Install them with pip:

pip install pandas xlsxwriter

XlsxWriter is a separate library from pandas. It works independently, but pandas uses it as an optional engine. If you only have pandas installed, the default engine is openpyxl (or xlwt for older .xls files). To use XlsxWriter, you must install it explicitly.

Gaining Control with pd.ExcelWriter

For most real-world exports, you need to format cells, set column widths, or add a title. That requires access to the underlying workbook and worksheet objects. Use pd.ExcelWriter to create a writer object and then retrieve the XlsxWriter objects:

import pandas as pd with pd.ExcelWriter("formatted.xlsx", engine="xlsxwriter") as writer: df.to_excel(writer, sheet_name="Sales", index=False) workbook = writer.book worksheet = writer.sheets["Sales"] # Add a number format for the price column price_format = workbook.add_format({"num_format": "$#,##0.00"}) worksheet.set_column("B:B", 12, price_format) # Set column widths worksheet.set_column("A:A", 20) worksheet.set_column("C:C", 10)

The with block ensures the file is closed and saved properly. After calling to_excel, writer.book and writer.sheets become available. writer.sheets is a dictionary mapping sheet names to worksheet objects.

You can apply formats to entire columns, as shown, or to specific cells using worksheet.write() with a format. This is the key difference from using to_excel directly: you can now style the output.

Writing Multiple DataFrames to One Workbook

A common requirement is to put several DataFrames into separate sheets of the same workbook. With pd.ExcelWriter, you simply call to_excel multiple times with different sheet_name values:

import pandas as pd sales = pd.DataFrame({"Region": ["North", "South"], "Revenue": [1000, 1500]}) costs = pd.DataFrame({"Region": ["North", "South"], "Cost": [600, 800]}) with pd.ExcelWriter("report.xlsx", engine="xlsxwriter") as writer: sales.to_excel(writer, sheet_name="Sales", index=False) costs.to_excel(writer, sheet_name="Costs", index=False) # Adjust each sheet individually for sheet_name in writer.sheets: worksheet = writer.sheets[sheet_name] worksheet.set_column("A:A", 15) worksheet.set_column("B:B", 12)

Each to_excel call creates a new sheet if the name doesn't exist. If you reuse a sheet name, pandas will overwrite it. This is useful for building multi-tab reports in a single pass.

Handling Data Types and Date/Time Values

XlsxWriter writes pandas data types to Excel cells with reasonable defaults, but date and time values require attention. When a DataFrame column contains datetime64 objects, pandas converts them to Excel serial dates. However, the cell format defaults to a number unless you explicitly set a date format.

import pandas as pd from datetime import datetime orders = pd.DataFrame({ "OrderDate": [datetime(2024, 1, 5), datetime(2024, 2, 14)], "Amount": [200, 350] }) with pd.ExcelWriter("dates.xlsx", engine="xlsxwriter") as writer: orders.to_excel(writer, sheet_name="Orders", index=False) workbook = writer.book worksheet = writer.sheets["Orders"] date_format = workbook.add_format({"num_format": "yyyy-mm-dd"}) worksheet.set_column("A:A", 12, date_format)

Without the num_format, the date cells display as numbers like 45256. Setting the column format to a date pattern makes them readable. This is a common pitfall when exporting DataFrames with datetime columns.

Performance and Memory Considerations for Large DataFrames

XlsxWriter is generally fast, but writing a very large DataFrame can consume significant memory because pandas holds the entire DataFrame in memory and XlsxWriter builds the workbook in memory as well. For datasets with millions of rows, you may need to write in chunks to avoid memory exhaustion.

One approach is to write the DataFrame in slices using the startrow and header parameters of to_excel:

import pandas as pd # Assume df is a large DataFrame chunk_size = 10000 with pd.ExcelWriter("large.xlsx", engine="xlsxwriter") as writer: for start in range(0, len(df), chunk_size): chunk = df.iloc[start:start+chunk_size] chunk.to_excel(writer, sheet_name="Data", startrow=start, header=(start == 0), index=False)

This writes each chunk starting at the appropriate row. The header argument is only True for the first chunk to avoid repeating the column headers. This reduces peak memory usage because only a chunk of the DataFrame is processed at a time.

XlsxWriter also supports the constant_memory option in its constructor, but pandas does not expose that directly. If you need that level of control, you can bypass pandas and use XlsxWriter's write methods directly, but that loses the convenience of DataFrame conversion.

Common Pitfalls and Compatibility Notes

Several behaviors can surprise developers new to this combination.

NaN values are written as empty cells by default. If you want a specific placeholder, you need to fill the DataFrame before writing:

df.fillna("N/A", inplace=True)

Formulas in DataFrame cells are written as literal strings. If you want an actual Excel formula, you must use worksheet.write_formula() manually after the DataFrame is written. For example, to add a total row:

worksheet.write_formula("D5", "=SUM(D2:D4)")

Column names with special characters are written as-is. If a column name contains spaces or starts with a digit, Excel will still display it, but it may cause issues if you later read the file with pandas and use df. attribute access.

Sheet name limits apply: Excel sheet names cannot exceed 31 characters and cannot contain certain characters like : \ / ? * [ ]. pandas will raise an error if you try to use an invalid name.

Compatibility with Excel versions: XlsxWriter produces .xlsx files that are compatible with Excel 2007 and later. It does not support the older .xls format. If you need .xls, you must use a different engine like xlwt.

By understanding these behaviors, you can avoid the most common issues when using XlsxWriter to create Excel files from pandas DataFrames. The combination gives you the convenience of pandas' data manipulation with the fine-grained control of a dedicated Excel writer.

python xlsxwriter create excel and write pandas dataframe: P | RYUSLOG DEV