Excel Automation With ChatGPT and Python: Five Examples
Five Excel jobs I automated by asking ChatGPT for the Python: find and replace across a folder of files, saving sheets as workbooks, splitting data by country, commenting and refactoring an existing script, and building PowerPoint slides. Each example shows what I asked for and where the code fell short.
Find and replace across files
ChatGPT wrote a working script on the first try for a find and replace across every worksheet of every file in a folder. I asked it to loop over the input folder, replace “Small Business” with “Small Market” and “Midmarket” with “Midsize Market”, and save each workbook to an output folder.

The script came with comments for each step. In the output folder both values were replaced in all worksheets. My hand-written version of this script is in find and replace values in multiple Excel files.
Save sheets as workbooks
Saving each worksheet as its own workbook worked, but the formatting was lost. I asked for one new workbook per sheet in a folder called “Output”, created if missing, with the original formatting kept.
The script ran without errors, but it used openpyxl and copied only the values. When I asked it to keep the formatting, the new versions threw errors, and after a few rounds it was clear that openpyxl is the wrong tool for this. xlwings copies sheets with their formatting because it drives Excel itself, so name the package in the prompt. The xlwings approach is in splitting each Excel sheet into a separate file.
Split data by country
Filtering one workbook into a file per country worked as asked. I asked ChatGPT to filter “Financial_Data.xlsx” to the year 2021, split it by the unique values in column B (“Country”) and save one workbook per country in an attachments folder.

ChatGPT used pandas and explained each step. Every new workbook held only the 2021 rows for its country. Here too, naming the package in the prompt steers the result. The same split, written without AI, is in splitting Excel data into multiple files by column values.
Comment and refactor code
ChatGPT is also useful on code you already have. I gave it an xlwings script of mine that saves each worksheet as a separate workbook and asked it to add comments. It commented every line, which is more than I would, but accurate.
Then I asked it to turn the script into a function. It did, with type hints and a docstring that explains the arguments. The original script is in my post on saving Excel sheets as separate workbooks.
Generate PowerPoint slides
A PowerPoint deck from Python is possible but needs layout work afterwards. I asked for a script that builds three slides, each with a title, three bullet points and an image from an API.

The code split the work into functions and came with a summary. The deck worked, but the layout did not match what I had in mind, so treat it as a starting point. If you only need to export Excel ranges and charts to PowerPoint, my Excel2PPT add-in does that without a script.
Prompting tips
Clear, specific prompts get working code. Name the folders, sheets, columns and the Python package you want, and split a big job into small tasks you can test one at a time. Expect to adjust the code, and check the output against the source files.
To read and fix that code with confidence, my Excel automation course teaches Python for Excel users from zero, including working with AI on the code. I tried the same idea with VBA in four macros ChatGPT wrote for me.
