Automate Excel with ChatGPT (and Python)

In this video, we’re going to make ChatGPT generate Python code in order to automate Excel.
If you want to test this out, download the files I used for this tutorial here:
Before running the scripts generated by chatGPT, make sure you have the libraries installed. If not, install them with pip.

🔗 Link:

My Free Cheatsheets
🤖 Python Automation:
🐍 Python for Data Science Cheat Sheet:
📐 R for Data Science:
🛢️ SQL for Data Science:
🌐 Web Scraping:
📘 My eBook “Code Less Earn More”:

Social Media
🎮 Discord:
🔗 LinkedIn:
🐥 Twitter:
✏️ Medium:
😺 GitHub:

My Courses
🔥 Join My Automation Course in Python:
🔥 Join My Python for Data Science Bootcamp:
🔥 8-hour Web Scraping Course in Python:
💰 Make money using your Programming & Data Science skills:

Support My Work
💵 PayPal:

0:00 Intro
0:15 Joining Excel files with ChatGPT
4:21 Applying formulas to multiple Excel files
7:57 Renaming files with ChatGPT
9:48 Editing the content in Excel using ChatGPT

In this video I’m going to show you how To use child DBT to generate python code That automates Microsoft Excel we’re Going to automate tasks such as joining Multiple Excel files applying formulas To multiple files and more so let’s get Started okay for this demo I’m going to Use this sales data that I have here These are sales data that are in Excel And we have sales data from January Until December so there are 12 Excel Files here and the first thing we’re Gonna automate is joining these Excel Files so we want to put all the Information from January until December In one single Excel file and you can do This manually or you can make chat GPT To generate python code to do this for You so let’s do it this way so first I’m Going to show you the content inside one Of these files so I’m gonna open this One and this is the preview and Basically this file has only sales data Uh in one file there are 1000 rows and If we join all of these files we should Have 12 000 rows so let’s make chat DBT Create a script that does all of this For us and here’s the prompt it says I Have 12 Excel files named and then I Write the name of the Excel files so Sales January sales February but instead Of writing all of them from January Until December I just write these three Dots and write that last name so sales

Underscore d e c that stands for December I’m gonna make chat DPT figure Out the name of the files let’s see if It can do it well and finally I say use Python to concatenate the data inside The 12 Excel files now I press enter and Now chat GPT should create an uh script That puts all the Excel files in one Single file so let’s see how it does This so here’s creating a list with the Name of the amount then is concatenating The strings with the name sales Underscore and well apparently Everything is working fine and at the End of the code this DF final is being Supported to Excel and this is the name Of the Excel file so this looks good This looks really good so now I’m just Gonna copy the code and I’m gonna paste It here So we’re using pandas for this then We’re creating lists and then we’re Exporting the final Data frame that has all the Excel files Concatenated So here is the code and now before I run This if you want to follow me you have To keep in mind that your python script Should be located in the same directory Where the Excel files are located so Here I have my python script and here Are all the Excel files so now I only Have to run this code and see what Happens and here I got an error and it

Says that the file is not on this Directory and well apparently chat DBT Didn’t generate the code that includes Also the extension so here we should Have something like this that Xlsx so with this we have the whole name Of the file so for example here sales Underscore January which is right here And we need this dot xlsx so here I’m Doing this manually but you can also Tell Chad gbt to fix its mistake for Example here it added the extension xlsx But here just for the sake of this video I’m doing this manually so I’m adding This and now we should have this file Concatenated underscore data generated So as you might remember this is the Name of the of the data frame that was Exported and well it’s right here so now I’m going to open this file and see if The 12 000 rows are in this file so Here’s the file and here is the header Now I’m going to the last row and as you Can see the last row is twelve thousand And one so we successfully concatenated The data inside all of these Excel files And we did this without writing code but Letting cat DPT generate all the python Code for us alright for the next Automation we’re going to apply formulas To multiple Excel files so we’re going To work with this sales data again and Let’s say we want to apply two formulas The sum formula and also the average

Formula and here we’re going to work With two columns the column eight and The column J and for the column H we’re Going to sum the values from H2 to H1001 and the result is going to be in The cell H1002 and we’re going to do the same for The column J but in this case we’re Going to calculate the average so Applying these formulas might look Simple when it’s only one file but when You have to do this to multiple files For example these files that you can see Here it’s very time consuming so we’re Gonna make chat DBT generate python code That applies these two formulas to all The files that you can see here so let’s Do this so I go to chat GPT and then I Write the following prompt so here I say I have multiple Excel files in my Directory use Python to apply the sound Formula from J2 to J1001 and then the average formula from H2 to H1001 write the results in the cell J1002 and H10002 respectively so with this we’re Gonna create a script that does all of This not only for one Excel file but for Every Excel file that is in my directory So now I press enter and let’s see the Code that chargpt is going to generate Okay chat GPT generated the code but This code doesn’t look good because here

Is not considering all the files that I Have in my directory so I’m going to Make chat dpz to use an alternative and I’m going to tell it uh use the glove Module okay I told chargpt to do it Without with the glove module but it Didn’t understand me so I tried again And apparently now it’s generating the Code I want it so sometimes it doesn’t Understand what you want to do or it Doesn’t do a good job so you have to try Again until you find that desired result So here this looks uh good apparently And what I’m gonna do is copy the code To test this out okay I pasted the code In Python I’m going to run this and here Apparently the code is not working so I’m gonna ask chat GPT to fix its Mistakes and I’ll be back in a second Okay tell DPT fix the errors and here’s The code that it generated so now I’m Gonna run this and hopefully we’re gonna Get the results that we were expecting So now these are finished with success Now I’m going back here to my folder and Now let’s see if the value is there okay I opened one of the files in my folder And now in column H and in column J we Should see that formula so now I scroll Down and here In h1002 we see that there is the Average formula and in J 1002 there is that sound formula so we Can see that the code is working so chat

DBT generated the code that added the Formulas to every single file that is Here so you don’t have to manually uh Write the formulas in every single file That is here but you can let tab DPT Generate python code that adds the Formulas to all the files for you Alright now what if you want to change All the names of the Excel files inside This folder right now as you can see the Name of the files is something like this Sales underscore and then the name of The month so we have from January until December but what if you want to add the Year 2022 in front of the name of the files So something like this 2022 underscore and then the current Name so you can do this man finally one By one for these 12 Excel files but if You will have something like 100 files This will be too tedious so we can make Cat GPT generate a script that changes The name of all the files with one click So now let’s do this here we can write The following prompt use Python to Rename all the files in my directory by Adding the number 2022 in front so I’m Going to press enter and see the code That chargpt is gonna generate first It’s filtering only those files that Have the xlsx extension and then is Adding that 2022 with the underscore in Front of the name of the file so here

The code is very simple I’m going to Copy it and I’m gonna paste it here so It’s very simple and I’m going to open This left panel to show you how the Names are going to change after I run This script so so right now the name of One of the file for example is sales Underscore APR and if I run this now the Name is 2022 underscore and sales Underscore APR so as you can see all the Names were changed and now we have the 2022 underscore at the beginning of each Name all right in the last automation Consists in editing every single Row in An Excel file for example here I have an Excel file with only phone numbers and What if we want to add the country code To each row in this file so for example Now we only have the numbers but we Don’t have the country code so if we Want to add the country code what we Have to do well in Excel is use the Right or actually that left in this case The left function ending applied in all The columns that might look very simple But when it comes to doing this for Multiple files for example here I have Two files but but you can have thousands Of files or hundreds of files This is Not Practical so what we can do is Generate code that edits every single Row in an Excel file and actually not Only one Excel file but in all the Excel Files inside a folder so let’s do this

In this case I’m going to add the Country code plus 1 to each phone number That is listed in this Excel file so Here’s the prompt that I’m going to Create the prompt test I have multiple CSV files actually I have CSV files in This case this is not a xlsx but it’s a CSV file but anyway it’s the same and Then I have multiple CSV files with only Phone numbers in my directory the phone Numbers are in a column name font so Here there is the the hello phone in This case the header is going to be the Name of the column in a data frame That’s why I’m specifying this and then I finish this by saying use Python to Add the country code Plus 1 to which Phone number in the CSV files and that’s Pretty much it alright GPT finished Generating the code and what it did was Explain the code into part the first Part is the function add country code And the second part is a for Loop uh It’s a for Loop where we can apply this Function to all the CSV files listed so What I’m going to do is first copy this Code and then go to uh here to Python And I’m going to open This folder and this empty script so Here I’m gonna paste the code And then I’m going to paste the other Code that is here so as I told you Before there are two parts and now I’m Gonna

Organize this much better so the library Should be on top and with this Everything should be ready So here I want to show you that this Python script is inside this folder Where there are my two CSV files so you Have to keep that in mind in case you Want to follow my steps so here now I’m Going to run this and hopefully this is Going to add that plus one country code To all the full numbers so apparently Everything was uh was successful and now Let’s open the CSV files actually I’m Just going to open the preview and as You can see here we have the phone Number but also we have the country code So plus one to every single row so it’s Not only for this file but also for this Second CSV file so here is the country Code and the phone number so with this We successfully created a script that Adds the country code to each row in our CSV files and we did this without Writing any code but letting chat GPT Generate all the python code for us hey Thanks for watching this video don’t Forget to give it a like And subscribe To this channel for more content like This that’s it for this video I’ll see You on the next one

You May Also Like

About the Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *