If you are a data analyst or scientist, the following is probably true: at some point in your work, you need to Generate Excel in Python. Perhaps your boss requires an Excel file every week with some data analysis results, or maybe your customer asks for an organized spreadsheet with data instead of a JSON file. Having the skills how to make Excel file in Python can make your scripts shareable in the most literal way, as they can output data into an organized business-ready document. In this article, , we will show you to generate excel file using Python script. Check out python pandas export to excel in several lines of code, as well as write data to Excel Python with formatting with XlsxWriter.
Ultimately, you will know how to create Excel file, save a dataframe to excel in python. Also how to create a generate excel report python script.
Why Excel is great for Python Reporting
Excel occupies the optimal point between programming professionals and the rest of us. You program in Python; your teammates work in Excel. When you export to spreadsheets, the whole team shares the same language.
Excel files are also easily transportable. Finance department, HR, marketing, accounts, and the operation departments of the UK, US, Canada, and Poland can easily open .xlsx files without the need for additional software. These can be sent through email, SharePoint, Google Drive, or a ticket.
Python deals with data better than doing the same tasks in Excel. It allows you to refine data with millions of rows and send only the final result to Excel. That leaves Excel to its strengths of being a spreadsheet program. Microsoft Excel also provides you with filters, graphs, pivot tables, and simple formulas. Once you set up the basic sheet with your code in python, you’re able to analyze data in your spreadsheet without interacting with code.
Real World Use Case for Excel in Python
You can use Python and Excel together in many everyday scenarios:
- Daily or weekly sales reports grouped by country or region
- Export of cleaned CRM contacts for email campaigns
- Budget vs actual spending sheets for finance teams
- Inventory and stock level reports for warehouses and shops
- Student grades or attendance sheets for schools and universities
- Log summaries from apps or servers turned into readable reports
Creating an Excel file using Pandas
Pandas gives you the fastest way to move from data to Excel. You usually work with DataFrames anyway, so python pandas export to excel feels natural.
Imagine you run a small online store and track daily orders in Python as a list. You want to send a simple Excel file to your accountant.
import pandas as pd
orders = [
["2026-01-01", "Order-001", "Alice", 120.5],
["2026-01-01", "Order-002", "Bob", 89.0],
["2026-01-02", "Order-003", "Carla", 45.25]
]
columns = ["date", "order_id", "customer", "amount"]
df = pd.DataFrame(orders, columns=columns)
df.to_excel("daily_orders.xlsx", index=False)
Here, script will create Excel file from list Python in three clear steps: build a list, turn it into a DataFrame, then export it. For this example we have used list with static data however in real world application these data comes from database.
Let’s take another example. Imagine you analyse web traffic. You pull data from an API, clean it in Pandas, then hand the final result to your marketing team.
import pandas as pd
data = {
"page": ["/", "/pricing", "/blog"],
"visits": [1500, 430, 980],
"signups": [75, 30, 22]
}
df = pd.DataFrame(data)
df.to_excel("web_traffic_report.xlsx", sheet_name="Traffic", index=False)
Here it will create data frame for these data and then convert it into excel file using to_excel() method.
Creating styled and formatted Excel files using XlsxWriter
Pandas handles structure well, but sometimes you want richer styling. Maybe you send a monthly dashboard to leadership and it needs colours, currency formats and clear highlights. With XlsxWriter, you write data to Excel Python with formatting in a very controlled way. You choose fonts, colours, borders and even conditional formatting.
Imagine a small consulting firm that tracks project revenue. They want a neat Excel report they can send directly to a client.
import xlsxwriter
workbook = xlsxwriter.Workbook("project_revenue.xlsx")
worksheet = workbook.add_worksheet("Revenue")
header_format = workbook.add_format(
{"bold": True, "bg_color": "#4F81BD", "font_color": "white", "align": "center"}
)
money_format = workbook.add_format({"num_format": "$#,##0.00"})
headers = ["Project", "Client", "Revenue"]
data = [
["Website redesign", "Client A", 12500],
["Mobile app", "Client B", 23000],
["Audit", "Client C", 4800]
]
for col, header in enumerate(headers):
worksheet.write(0, col, header, header_format)
for row, record in enumerate(data, start=1):
worksheet.write(row, 0, record[0])
worksheet.write(row, 1, record[1])
worksheet.write(row, 2, record[2], money_format)
worksheet.set_column(0, 1, 20)
worksheet.set_column(2, 2, 15)
workbook.close()
With this approach, file already looks like a small report, not just a raw export. The client opens it and sees bold headers, readable columns and proper currency formatting.
Highlight important values with conditional formatting
Imagine, you want to highlight some value based on some conditions like top performers or low-performers. Let’s take an example of retail chain that wants to spot low‑performing stores quickly. You can highlight stores that sell under a threshold.
import xlsxwriter
workbook = xlsxwriter.Workbook("store_performance.xlsx")
worksheet = workbook.add_worksheet("Stores")
headers = ["Store", "City", "Monthly sales"]
data = [
["Store 1", "Toronto", 52000],
["Store 2", "Vancouver", 31000],
["Store 3", "Calgary", 18000],
["Store 4", "Montreal", 42000]
]
for col, header in enumerate(headers):
worksheet.write(0, col, header)
for row, record in enumerate(data, start=1):
worksheet.write_row(row, 0, record)
worksheet.conditional_format(
1,
2,
len(data),
2,
{
"type": "cell",
"criteria": "<",
"value": 30000,
"format": workbook.add_format({"bg_color": "#FFC7CE"})
}
)
workbook.close()
When the someone opens this sheet, low‑sales stores stand out in red. You answer a key business question in seconds. You can also add for top performer too with green color.
Conclusion
When you know how to create an Excel file in Python, you turn raw scripts into tools real people can use. Pandas gives you a quick way to build and export tables, while XlsxWriter will let you write data to Excel Python with professional formatting.
Be it just moving a DataFrame to Excel, or you want a full, styled report, Python has it all covered. You script the logic once, then Generate Excel in Python again and again for different dates, regions, or clients with almost no extra effort.
