├── 01_find-replace-values-in-excel ├── ChatGPT_Prompt.txt ├── Input │ ├── Canada.xlsx │ ├── France.xlsx │ ├── Germany.xlsx │ ├── Mexico.xlsx │ └── United States Of America.xlsx ├── find_replace_chatgpt.py └── find_replace_myversion.py ├── 02_split-excel-sheet-into-workbook-only-values ├── ChatGPT_Prompt.txt ├── Output │ ├── Canada.xlsx │ ├── France.xlsx │ ├── Germany.xlsx │ ├── Mexico.xlsx │ └── United States Of America.xlsx ├── data.xlsx ├── split_wb_to_sheets.py └── split_wb_to_sheets_only_values_but_keep_formatting.py ├── 03_filter-export-excel-files ├── Attachments │ ├── Canada.xlsx │ ├── France.xlsx │ ├── Germany.xlsx │ ├── Japan.xlsx │ └── United States of America.xlsx ├── ChatGPT_Prompt.txt ├── Financial_Data.xlsx └── split_data_into_sheets.py ├── 04_split-ws-into-wb ├── ChatGPT_Prompt.txt ├── Financial_Data.xlsx ├── split_ws_into_wb.py └── split_ws_into_wb_chatgpt.py ├── 05_powerpoint-automation ├── ChatGPT_Prompt.txt ├── dog.jpg ├── my-awesome-ppt.pptx └── powerpoint-automation.py └── README.md /01_find-replace-values-in-excel/ChatGPT_Prompt.txt: -------------------------------------------------------------------------------- 1 | Here is the chat history for the task: https://chatgpt-static.s3.amazonaws.com/chats/gg5662.html -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/Input/Canada.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/01_find-replace-values-in-excel/Input/Canada.xlsx -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/Input/France.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/01_find-replace-values-in-excel/Input/France.xlsx -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/Input/Germany.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/01_find-replace-values-in-excel/Input/Germany.xlsx -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/Input/Mexico.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/01_find-replace-values-in-excel/Input/Mexico.xlsx -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/Input/United States Of America.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/01_find-replace-values-in-excel/Input/United States Of America.xlsx -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/find_replace_chatgpt.py: -------------------------------------------------------------------------------- 1 | import os 2 | import openpyxl 3 | 4 | # Set the input and output directories 5 | input_dir = 'Input' 6 | output_dir = 'Output' 7 | 8 | # Create the output directory if it does not exist 9 | if not os.path.exists(output_dir): 10 | os.makedirs(output_dir) 11 | 12 | # Iterate through all the Excel files in the input directory 13 | for file in os.listdir(input_dir): 14 | # Check if the file is an Excel file 15 | if file.endswith('.xlsx'): 16 | # Open the workbook 17 | wb = openpyxl.load_workbook(os.path.join(input_dir, file)) 18 | 19 | # Iterate through all the sheets in the workbook 20 | for sheet in wb.worksheets: 21 | # Iterate through all the cells in the sheet 22 | for row in sheet.rows: 23 | for cell in row: 24 | # Replace the text if it matches 25 | if cell.value == 'Small Business': 26 | cell.value = 'Small Market' 27 | elif cell.value == 'Midmarket': 28 | cell.value = 'Midsize Market' 29 | 30 | # Save the modified workbook to the output directory 31 | wb.save(os.path.join(output_dir, file)) 32 | -------------------------------------------------------------------------------- /01_find-replace-values-in-excel/find_replace_myversion.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import openpyxl 3 | 4 | BASE_DIR = Path(__file__).parent 5 | INPUT_DIR = BASE_DIR / "Input" 6 | OUTPUT_DIR = BASE_DIR / "Output" 7 | 8 | # Create output directory 9 | OUTPUT_DIR.mkdir(parents=True, exist_ok=True) 10 | 11 | replacement_pair = {"Small Business": "Small Market", "Midmarket": "Midsize Market"} 12 | 13 | files = list(INPUT_DIR.rglob("*.xls*")) 14 | for file in files: 15 | wb = openpyxl.load_workbook(file) 16 | for ws in wb.worksheets: 17 | # Iterate over the columns and rows, search for the text and replace 18 | for row in ws.iter_rows(): 19 | for cell in row: 20 | if cell.value in replacement_pair.keys(): 21 | cell.value = replacement_pair.get(cell.value) 22 | wb.save(OUTPUT_DIR / f"{file.stem}_NEW.xlsx") -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/ChatGPT_Prompt.txt: -------------------------------------------------------------------------------- 1 | Here is the chat history for the task: https://chatgpt-static.s3.amazonaws.com/chats/dv3385.html -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/Output/Canada.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/Output/Canada.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/Output/France.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/Output/France.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/Output/Germany.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/Output/Germany.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/Output/Mexico.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/Output/Mexico.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/Output/United States Of America.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/Output/United States Of America.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/02_split-excel-sheet-into-workbook-only-values/data.xlsx -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/split_wb_to_sheets.py: -------------------------------------------------------------------------------- 1 | import os 2 | import openpyxl 3 | 4 | # open workbook 5 | wb = openpyxl.load_workbook('data.xlsx') 6 | 7 | # create 'Output' folder if it doesn't exist 8 | if not os.path.exists('Output'): 9 | os.makedirs('Output') 10 | 11 | # save each worksheet as a new workbook 12 | for sheet in wb: 13 | wb_temp = openpyxl.Workbook() 14 | ws_temp = wb_temp.active 15 | for row in sheet: 16 | for cell in row: 17 | ws_temp[cell.coordinate].value = cell.value 18 | ws_temp[cell.coordinate].font = cell.font 19 | ws_temp[cell.coordinate].border = cell.border 20 | ws_temp[cell.coordinate].fill = cell.fill 21 | ws_temp[cell.coordinate].number_format = cell.number_format 22 | ws_temp[cell.coordinate].protection = cell.protection 23 | ws_temp[cell.coordinate].alignment = cell.alignment 24 | wb_temp.save(f'Output/{sheet.title}.xlsx') 25 | 26 | # close workbook 27 | wb.close() 28 | -------------------------------------------------------------------------------- /02_split-excel-sheet-into-workbook-only-values/split_wb_to_sheets_only_values_but_keep_formatting.py: -------------------------------------------------------------------------------- 1 | # 👉 Save each Excel sheet as separate file. Copy only the values (keep the formatting!) 2 | from pathlib import Path 3 | 4 | import xlwings as xw 5 | 6 | BASE_DIR = Path(__file__).parent if "__file__" in locals() else Path.cwd() 7 | EXCEL_FILE = BASE_DIR / "data.xlsx" 8 | OUTPUT_DIR = BASE_DIR / "Output" 9 | 10 | # Create Output directory 11 | OUTPUT_DIR.mkdir(parents=True, exist_ok=True) 12 | 13 | with xw.App(visible=False) as app: 14 | wb = app.books.open(EXCEL_FILE) 15 | for sheet in wb.sheets: 16 | # Create a new workbook 17 | wb_new = app.books.add() 18 | 19 | # Copy the orginal sheet 20 | sheet.copy(after=wb_new.sheets[0]) 21 | 22 | # Clear only the contents 23 | wb_new.sheets[1].used_range.clear_contents() 24 | 25 | # Delete the inital first sheet when creating the wb 26 | wb_new.sheets[0].delete() 27 | 28 | # Get the address from the used_range object 29 | rng_address = sheet.used_range.get_address() 30 | 31 | # Transfer values within used range to new workbook 32 | wb_new.sheets[0].range(rng_address).value = sheet.range(rng_address).value 33 | 34 | # Save & close workbook 35 | wb_new.save(OUTPUT_DIR / f'{sheet.name}.xlsx') 36 | wb_new.close() -------------------------------------------------------------------------------- /03_filter-export-excel-files/Attachments/Canada.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Attachments/Canada.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/Attachments/France.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Attachments/France.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/Attachments/Germany.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Attachments/Germany.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/Attachments/Japan.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Attachments/Japan.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/Attachments/United States of America.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Attachments/United States of America.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/ChatGPT_Prompt.txt: -------------------------------------------------------------------------------- 1 | Here is the chat history for the task: https://chatgpt-static.s3.amazonaws.com/chats/ag54456.html -------------------------------------------------------------------------------- /03_filter-export-excel-files/Financial_Data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/03_filter-export-excel-files/Financial_Data.xlsx -------------------------------------------------------------------------------- /03_filter-export-excel-files/split_data_into_sheets.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | 4 | # Load the Data sheet in the Financial_Data.xlsx workbook 5 | df = pd.read_excel('Financial_Data.xlsx', sheet_name='Data') 6 | 7 | # Filter the data for the year 2021 8 | df = df[df['Year'] == 2021] 9 | 10 | # Get a list of unique countries 11 | countries = df['Country'].unique() 12 | 13 | # Create the Attachments folder if it doesn't already exist 14 | if not os.path.exists('Attachments'): 15 | os.makedirs('Attachments') 16 | 17 | # Iterate through each country 18 | for country in countries: 19 | # Extract the financial data for this country 20 | country_df = df[df['Country'] == country] 21 | 22 | # Select the columns from A to P 23 | country_df = country_df.iloc[:, :16] 24 | 25 | # Save the data for this country to an Excel file 26 | country_df.to_excel(f'Attachments/{country}.xlsx', index=False) 27 | -------------------------------------------------------------------------------- /04_split-ws-into-wb/ChatGPT_Prompt.txt: -------------------------------------------------------------------------------- 1 | Here is the chat history for the task: https://chatgpt-static.s3.amazonaws.com/chats/uo91917.html -------------------------------------------------------------------------------- /04_split-ws-into-wb/Financial_Data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/04_split-ws-into-wb/Financial_Data.xlsx -------------------------------------------------------------------------------- /04_split-ws-into-wb/split_ws_into_wb.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | import xlwings as xw # pip install xlwings 4 | 5 | this_dir = Path(__file__).parent if '__file__' in locals() else Path.cwd() 6 | excel_file = this_dir / 'Financial_Data.xlsx' 7 | output_dir = this_dir / 'Output' 8 | 9 | # Create Output directory 10 | output_dir.mkdir(parents=True, exist_ok=True) 11 | 12 | with xw.App(visible=False) as app: 13 | wb = app.books.open(excel_file) 14 | for sheet in wb.sheets: 15 | wb_new = app.books.add() 16 | sheet.copy(after=wb_new.sheets[0]) 17 | wb_new.sheets[0].delete() 18 | wb_new.save(output_dir / f'{sheet.name}.xlsx') 19 | wb_new.close() -------------------------------------------------------------------------------- /04_split-ws-into-wb/split_ws_into_wb_chatgpt.py: -------------------------------------------------------------------------------- 1 | import xlwings as xw # Library for interacting with Excel files 2 | from pathlib import Path # Library for working with file paths 3 | 4 | def split_excel_workbook(input_file: str, output_dir: str): 5 | """Splits an Excel workbook into multiple Excel files, with each file containing one sheet from the original workbook. 6 | The sheet name is used as the file name for each output file. 7 | 8 | Args: 9 | input_file (str): Path to the input Excel file. 10 | output_dir (str): Path to the output directory. 11 | """ 12 | # Set the path to the input Excel file 13 | excel_file = Path(input_file) 14 | 15 | # Set the path to the output directory 16 | output_dir = Path(output_dir) 17 | 18 | # Create the output directory if it doesn't exist 19 | output_dir.mkdir(parents=True, exist_ok=True) 20 | 21 | # Open the input Excel file and create a new hidden Excel app 22 | with xw.App(visible=False) as app: 23 | wb = app.books.open(excel_file) 24 | 25 | # Iterate through each sheet in the input workbook 26 | for sheet in wb.sheets: 27 | # Create a new workbook 28 | wb_new = app.books.add() 29 | 30 | # Copy the sheet from the input workbook to the new workbook 31 | sheet.copy(after=wb_new.sheets[0]) 32 | 33 | # Delete the default sheet in the new workbook 34 | wb_new.sheets[0].delete() 35 | 36 | # Save the new workbook with the sheet name as the file name 37 | wb_new.save(output_dir / f'{sheet.name}.xlsx') 38 | 39 | # Close the new workbook 40 | wb_new.close() 41 | 42 | # Example usage 43 | split_excel_workbook('Financial_Data.xlsx', 'Output') 44 | -------------------------------------------------------------------------------- /05_powerpoint-automation/ChatGPT_Prompt.txt: -------------------------------------------------------------------------------- 1 | Here is the chat history for the task: https://chatgpt-static.s3.amazonaws.com/chats/zd62125.html -------------------------------------------------------------------------------- /05_powerpoint-automation/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/05_powerpoint-automation/dog.jpg -------------------------------------------------------------------------------- /05_powerpoint-automation/my-awesome-ppt.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/excel-automation-with-chatgpt/0f0075bb8258050728705391fd0af8dd5585c9ce/05_powerpoint-automation/my-awesome-ppt.pptx -------------------------------------------------------------------------------- /05_powerpoint-automation/powerpoint-automation.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from pptx import Presentation 3 | from pptx.enum.shapes import MSO_SHAPE 4 | from pptx.util import Inches 5 | 6 | def create_presentation(): 7 | prs = Presentation() 8 | return prs 9 | 10 | def add_title_slide(prs, title, bullet_points, image_url): 11 | slide = prs.slides.add_slide(prs.slide_layouts[0]) 12 | shapes = slide.shapes 13 | title_shape = shapes.title 14 | body_shape = shapes.placeholders[1] 15 | 16 | title_shape.text = title 17 | tf = body_shape.text_frame 18 | for point in bullet_points: 19 | tf.add_paragraph().text = point 20 | 21 | # download image and add it to the slide 22 | image_data = requests.get(image_url).content 23 | with open('dog.jpg', 'wb') as f: 24 | f.write(image_data) 25 | left = Inches(5.5) 26 | top = Inches(2) 27 | height = Inches(1.5) 28 | pic = slide.shapes.add_picture('dog.jpg', left, top, height=height) 29 | 30 | def main(): 31 | prs = create_presentation() 32 | bullet_points = ['First bullet point', 'Second bullet point', 'Third bullet point'] 33 | image_url = 'https://place.dog/300/300' 34 | add_title_slide(prs, 'Introduction', bullet_points, image_url) 35 | add_title_slide(prs, 'Methods', bullet_points, image_url) 36 | add_title_slide(prs, 'Results', bullet_points, image_url) 37 | 38 | prs.save('my-awesome-ppt.pptx') 39 | 40 | if __name__ == '__main__': 41 | main() 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Excel Automation Made Easy with Python and ChatGPT 2 | In this video, I'm sharing my experience with ChatGPT, an AI tool that helps you automate tasks in Python and Excel. I've been playing around with it over the past few weeks, and I have to say, it's pretty impressive. 3 | In the video, I'll be demonstrating a couple of practical examples of how you can use ChatGPT to make your life easier. Specifically, I'll be showing you how to use ChatGPT to replace certain phrases in multiple Excel files, save each worksheet in a workbook as a new workbook, and filter data in an Excel file by unique values. 4 | For some examples, I'll also be comparing the solution that ChatGPT gives me to the one I came up with in a previous tutorial. This way, you can see the differences and how ChatGPT's solution stacks up. 5 | 6 | ## Video Tutorial 7 | [![YouTube Video](https://img.youtube.com/vi/mMkVdlHXcjo/0.jpg)](https://youtu.be/mMkVdlHXcjo) 8 | 9 | 10 | 11 | 12 | ## 🤓 Check Out My Excel Add-ins 13 | I've developed some handy Excel add-ins that you might find useful: 14 | 15 | - 📊 **[Dashboard Add-in](https://pythonandvba.com/grafly)**: Easily create interactive and visually appealing dashboards. 16 | - 🎨 **[Cartoon Charts Add-In](https://pythonandvba.com/cuteplots)**: Create engaging and fun cartoon-style charts. 17 | - 🤪 **[Emoji Add-in](https://pythonandvba.com/emojify)**: Add a touch of fun to your spreadsheets with emojis. 18 | - 🛠️ **[MyToolBelt Add-in](https://pythonandvba.com/mytoolbelt)**: A versatile toolbelt for Excel, featuring: 19 | - Creation of Pandas DataFrames and Jupyter Notebooks from Excel ranges 20 | - ChatGPT integration for advanced data analysis 21 | - And much more! 22 | 23 | 24 | 25 | ## 🤝 Connect with Me 26 | - 📺 **YouTube:** [CodingIsFun](https://youtube.com/c/CodingIsFun) 27 | - 🌐 **Website:** [PythonAndVBA](https://pythonandvba.com) 28 | - 💬 **Discord:** [Join our Community](https://pythonandvba.com/discord) 29 | - 💼 **LinkedIn:** [Connect with me](https://www.linkedin.com/in/sven-bosau/) 30 | - 📸 **Instagram:** [Follow me](https://www.instagram.com/codingisfun_official/) 31 | 32 | ## Support My Work 33 | Love my content and want to show appreciation? Why not [buy me a coffee](https://pythonandvba.com/coffee-donation) to fuel my creative engine? Your support means the world to me! 😊 34 | 35 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://pythonandvba.com/coffee-donation) 36 | 37 | ## Feedback 38 | Got some thoughts or suggestions? Don't hesitate to reach out to me at contact@pythonandvba.com. I'd love to hear from you! 💡 39 | ![Logo](https://www.pythonandvba.com/banner-img) 40 | --------------------------------------------------------------------------------