How to Save DataFrame to Excel in Python

Cody Schneider

Moving your data analysis from a Python script to a shareable Excel spreadsheet is a crucial last step for many projects. Instead of just exporting raw data, you need a clean, professional-looking report that your team or clients can easily understand. This tutorial will walk you through saving your pandas DataFrames to Excel, from the simplest one-sheet export to creating multi-sheet workbooks with custom formatting.

Getting Your Environment Ready

Before we start exporting, you need a couple of things in place. First, you'll need the pandas library, which is the foundation for data manipulation in Python. If you don't have it installed, open your terminal or command prompt and run this simple command:

Next, pandas needs an engine to actually write Excel files. Think of it like this: pandas knows what to write, but it needs a helper library to know how to write the .xlsx file. The two most popular engines are openpyxl and XlsxWriter. We'll install both, as they have different strengths that we'll explore.

With that, you're all set. Let's create a sample DataFrame that we can use for all the examples in this guide.

The Simplest Method: Saving a DataFrame to a Single Excel Sheet

The most common task is saving a single DataFrame to an Excel file. Pandas makes this incredibly straightforward with the to_excel() method. Just call it on your DataFrame and provide a file name.

If you open the newly created simple_sales_report.xlsx file, you'll see your data. However, you'll probably notice an extra, unnamed column at the beginning. This is the DataFrame's index. While the index is useful within pandas, it often just adds clutter to a final report.

Fortunately, turning it off is easy. Just add the index=False argument:

That looks much cleaner! You can also customize the name of the sheet inside the Excel file using the sheet_name argument. If you don't provide one, it defaults to 'Sheet1'.

Writing Multiple DataFrames into One Excel File

What if you have several related DataFrames — say, sales, marketing costs, and inventory levels — and you want them all in one organized workbook, each on its own sheet? Exporting them one by one would create three separate files. Instead, you can use the pd.ExcelWriter object to manage a single file and direct each DataFrame to a specific sheet.

First, let's create two more sample DataFrames so we have something to work with:

Now, we can use pd.ExcelWriter to put them all together. The recommended approach is to use a with statement, which automatically handles closing and saving the file once you're done.

Let's break down what's happening here:

  1. with pd.ExcelWriter('comprehensive_report.xlsx') as writer: — creates an Excel writer object named writer that points to our target file.

  2. df_sales.to_excel(writer, ...): — calls to_excel() on each DataFrame, passing the writer object instead of a filename.

  3. sheet_name='...' — assigns a unique sheet name for each DataFrame to keep them organized.

When the with block finishes, Python automatically saves the workbook with all three sheets. This method is the clean, standard way to create organized, multi-sheet reports.

Advanced Control: Formatting and Styling Your Excel Output

Exporting data is useful, but professional reports often require formatting to make them readable and highlight key information. This is where the XlsxWriter engine truly shines. We can access its features directly through our pandas ExcelWriter to apply number formats, change column widths, and style headers.

Here's how you can create the same multi-sheet report but add some useful formatting.

1. Adjusting Column Widths Automatically

One of the most common annoyances is that default Excel columns are often too narrow, cutting off text. We can programmatically set the width of each column to fit its content. In this example, we'll write a simple loop that finds the length of the longest item in each column (including the header) and sets the column width accordingly.

2. Adding Number Formats and Header Styles

Let's take it a step further. We want to format our SalePrice column as currency and make our header bold with a background color. To do this, we create "format objects" using the workbook and then apply them.

Since applying formats column-by-column requires a bit more manual control, we'll only format our Sales sheet here.

In this more advanced script, we explicitly wrote the header so we could apply our custom header_format. We then told pandas to write the DataFrame data starting from the second row (startrow=1) and applied our currency format to the entire 'SalePrice' column. This gives you fine-grained control to produce presentation-ready reports directly from Python.

More Tips and Common Operations

Here are a few more frequent scenarios and how to handle them.

Appending a Sheet to an Existing Excel File

Sometimes you need to add a new sheet to a workbook that already exists without overwriting it. You can do this by setting the mode to 'a' for append and specifying openpyxl as the engine. Note that duplicates will cause issues so using if_sheet_exists='replace' is your friend.

Writing Data at a Specific Starting Point

If you want to add a title or some introductory text above your table, you can use the startrow and startcol arguments to offset where your DataFrame is written in the sheet.

This will leave two empty rows at the top and one empty column on the left.

Final Thoughts

Mastering how to export DataFrames to Excel is a practical skill that closes the loop between data analysis and business communication. We've seen how to use df.to_excel() for quick exports, combine multiple DataFrames into a single file with pd.ExcelWriter, and apply custom formatting with the XlsxWriter engine to create truly polished reports.

While automating reports with Python is a huge step up from manual copying and pasting, it still often involves a cycle of running a script, saving a file, and emailing it to stakeholders for review. This is where tools for creating connected, real-time dashboards can change the game entirely. With Graphed, we help you connect directly to your data sources and build live, interactive dashboards using simple natural language. Instead of exporting static .xlsx files, you can ask questions like "show me sales by region for this quarter" and instantly get a sharable visualization that's always up to date — no scripts required.