Automate Excel With Python and GPT-4o
ChatGPT can write the Python that automates your Excel work. Here GPT-4o merges several workbooks into one, builds an interactive calendar heatmap from the result, and then the two scripts are combined into a single job. The key is a precise prompt that describes the file structure.
The workflow
ChatGPT accepts screenshots and spreadsheets as input, so I show it the structure of my files, ask for a script, run it locally and check the output against the source data. I recorded this with GPT-4o; the same steps work with the newer models ChatGPT runs today.
Merge the workbooks
The data folder holds one sales workbook per city, all with the same columns. The first task is to merge them into a single workbook. I open one file and take a screenshot of the first rows so ChatGPT can see the column names and formats.

I paste the screenshot into ChatGPT with a prompt that names the folder and asks for one merged workbook. It returns a script and instructions. Because every file has the same layout, the screenshot was not strictly needed here, but it helps as soon as the columns are less obvious.

I save the code as a Python file and run it. To check the result, I filter the city column in the new workbook: all three cities are there.
import pandas as pd
import os
# Folder containing the Excel files
data_folder = 'data'
# Output file path
output_file = 'merged_data.xlsx'
# List to store dataframes
dfs = []
# Iterate through all files in the data folder
for file in os.listdir(data_folder):
if file.endswith('.xlsx'):
# Read the Excel file
file_path = os.path.join(data_folder, file)
df = pd.read_excel(file_path)
dfs.append(df)
# Concatenate all dataframes
merged_df = pd.concat(dfs, ignore_index=True)
# Save the merged dataframe to a new Excel file
merged_df.to_excel(output_file, index=False)
print(f"Merged data saved to {output_file}")
If you would rather write this part yourself, combining all Excel files in a folder into one workbook explains the code line by line.
Build a calendar heatmap
With the data in one workbook, I chart the 2023 sales per day as a calendar heatmap with PyEcharts, one interactive HTML file per city. Giving ChatGPT a working example from the PyEcharts gallery gets far better code than describing the chart in words.

I paste the gallery code into ChatGPT together with the merged workbook and ask for one heatmap per city. The script writes an HTML file for each city. I open one and compare a few days with the workbook to make sure the numbers match.

To build the same chart type with Plotly instead, see the calendar heatmap tutorial.
Combine both scripts
Once both scripts work, I ask ChatGPT to merge them into one, so a single run produces the merged workbook and every heatmap. I also asked it to use pandas method chaining for the data preparation, which reads much like a Power Query step list. This is the result:
import pandas as pd
from pathlib import Path
import datetime
import pyecharts.options as opts
from pyecharts.charts import Calendar
# Folder containing the Excel files
data_folder = Path("data")
# Output file path for merged data
output_file = Path("merged_data.xlsx")
# Concatenate all dataframes
merged_df = pd.concat(
[pd.read_excel(file) for file in data_folder.glob("*.xlsx")], ignore_index=True
)
# Save the merged dataframe to a new Excel file
merged_df.to_excel(output_file, index=False)
print(f"Merged data saved to {output_file}")
# Load the merged data and prepare for visualization
grouped_data = (
pd.read_excel(output_file)
.assign(
date_of_sale=lambda df: pd.to_datetime(df["date_of_sale"], format="%d/%m/%Y")
)
.query("date_of_sale.dt.year == 2023")
.groupby(["city", "date_of_sale"])["sales_amount"]
.sum()
.reset_index()
)
# Create a calendar heatmap for each city
for city in grouped_data["city"].unique():
city_data = grouped_data[grouped_data["city"] == city]
data = [
[date.strftime("%Y-%m-%d"), sales]
for date, sales in zip(city_data["date_of_sale"], city_data["sales_amount"])
]
# Define the calendar heatmap
calendar = (
Calendar()
.add(
series_name="Sales",
yaxis_data=data,
calendar_opts=opts.CalendarOpts(
pos_top="120",
pos_left="30",
pos_right="30",
range_="2023",
yearlabel_opts=opts.CalendarYearLabelOpts(is_show=False),
),
)
.set_global_opts(
title_opts=opts.TitleOpts(
pos_top="30", pos_left="center", title=f"2023 Sales Data for {city}"
),
visualmap_opts=opts.VisualMapOpts(
max_=grouped_data["sales_amount"].max(),
min_=grouped_data["sales_amount"].min(),
orient="horizontal",
is_piecewise=False,
),
)
)
# Save the heatmap as an HTML file
output_file = Path(f"{city}_2023_sales_heatmap.html")
calendar.render(output_file)
print("Calendar heatmaps created for each city.")
Prompting tips
The more precisely the prompt describes the files, the better the code. Name the folder, the sheet and the columns, say what the output should be, and attach a screenshot or the file when the layout is not obvious.
- Ask for one task per prompt, get it working, then ask ChatGPT to combine the scripts.
- For charts, paste a working example from the library’s gallery instead of describing the chart.
- Always check a few numbers in the output against the source workbook.
More examples with the exact prompts are in Excel automation with ChatGPT and Python.
