Automate Boring Office Tasks with ChatGPT and Python
Table of Contents
Introduction
This tutorial provides step-by-step instructions on how to automate various office tasks using ChatGPT and Python. By leveraging these tools, you can streamline processes in Outlook, create PowerPoint presentations, generate interactive charts, merge PDFs, compose emails, and even build websites. This guide will help you implement these automations effectively.
Chapter 1: Distribute Excel Files with Outlook
To automate the distribution of Excel files via Outlook:
-
Prepare Your Data
- Ensure you have an Excel workbook named
financial data.xlsxwith a sheet calledemail list. - In the
email listsheet, columns should include:- Column A: Recipient Name
- Column B: Recipient Email
- Column C: CC Email
- Column D: Attachment File Name
- Ensure you have an Excel workbook named
-
Create a Python Script
- Use ChatGPT to generate a Python script with the following functionality:
- Read data from the
email listsheet. - Compose Outlook emails using the recipient's name and email addresses.
- Attach files located in an
attachmentsfolder in the same directory.
- Read data from the
- Use ChatGPT to generate a Python script with the following functionality:
-
Sample Code
import pandas as pd import win32com.client as win32 data = pd.read_excel('financial data.xlsx', sheet_name='email list') outlook = win32.Dispatch('outlook.application') for index, row in data.iterrows(): mail = outlook.CreateItem(0) mail.To = row['Recipient Email'] mail.CC = row['CC Email'] mail.Subject = 'Financial Data' mail.Body = f"Hello {row['Recipient Name']},\n\nPlease find the financial data attached." attachment_path = f"attachments/{row['Attachment File Name']}" mail.Attachments.Add(attachment_path) mail.Display() # Opens the email without sending it -
Run the Script
- Execute the script. Outlook will create emails with the specified attachments.
-
Handling Errors
- If you encounter warnings, consult ChatGPT for modified code to address them.
Chapter 2: Automate PowerPoint Creation
To automate the creation of a PowerPoint presentation:
-
Prepare Your Data
- Ensure you have Excel files in a specified input folder containing sales data.
-
Define the Automation Steps
- The script should:
- Read all Excel files in the input folder.
- Exclude missing values and group sales by product.
- Generate bar charts using a specified library (e.g.,
matplotliborplotly).
- The script should:
-
Sample Code
import pandas as pd import matplotlib.pyplot as plt from pptx import Presentation input_folder = 'input/' chart_folder = 'charts/' # Create charts from Excel files for file in os.listdir(input_folder): data = pd.read_excel(os.path.join(input_folder, file)) grouped_data = data.groupby('Product')['Sales'].sum() plt.bar(grouped_data.index, grouped_data.values) plt.savefig(f"{chart_folder}/{file}.png") plt.close() # Create PowerPoint presentation prs = Presentation() for chart_file in os.listdir(chart_folder): slide = prs.slides.add_slide(prs.slide_layouts[5]) # Blank slide slide.shapes.add_picture(os.path.join(chart_folder, chart_file), left=0, top=0) prs.save('Financial_data.pptx') -
Run the Script
- Execute the script to generate a PowerPoint presentation with the created charts.
Chapter 3: Create Interactive Charts from Excel Data
To create an interactive chart:
-
Prepare Your Data
- Use the same sales data from previous examples.
-
Define the Automation Steps
- Sum up the sales for each country.
- Generate an interactive chart with custom color codes.
-
Sample Code
import pandas as pd import plotly.express as px data = pd.read_excel('financial data.xlsx') sales_summary = data.groupby('Country')['Sales'].sum().reset_index() fig = px.bar(sales_summary, x='Country', y='Sales', title='Sales by Country', color_discrete_sequence=['#FF5733']) fig.write_html('interactive_chart.html') -
Run the Script
- Execute the script to generate an HTML file with the interactive chart.
Chapter 4: Merge PDFs
To merge PDF files from two folders:
-
Prepare Your Data
- Ensure you have a
coverfolder with cover PDFs and adraftfolder with corresponding PDFs.
- Ensure you have a
-
Define the Automation Steps
- Match cover PDFs with draft PDFs based on naming conventions (e.g., numerical names).
-
Sample Code
from PyPDF2 import PdfMerger import os merger = PdfMerger() cover_folder = 'cover/' draft_folder = 'draft/' output_folder = 'output/' for cover_file in os.listdir(cover_folder): draft_file = f"{cover_file.split('.')[0]}.pdf" # Assuming naming convention merger.append(os.path.join(cover_folder, cover_file)) merger.append(os.path.join(draft_folder, draft_file)) merger.write(os.path.join(output_folder, 'merged.pdf')) merger.close() -
Run the Script
- Execute the script to create a merged PDF file.
Chapter 5: Write Emails
To compose a well-written email:
-
Define Your Request
- Use ChatGPT to generate an email requesting a salary increase or any other formal request.
-
Sample Email Prompt
- Provide ChatGPT with context about your work and reasons for your request.
-
Receive and Edit the Email
- Review the generated email, make adjustments if necessary, and send it through your preferred email client.
Chapter 6: Create a Streamlit Website
To create a simple website:
-
Use ChatGPT to Generate Code
- Request a Streamlit application code to showcase your work or automate tasks.
-
Sample Code
import streamlit as st st.title('Automated Office Tasks') st.write('This app demonstrates how to automate various office tasks.') -
Run the Application
- Execute the Streamlit application and access it via your web browser.
Conclusion
This guide has covered various methods to automate tedious office tasks using ChatGPT and Python. By implementing these steps, you can significantly improve efficiency in your workplace. Experiment with the provided scripts and modify them to suit your specific needs. Happy automating!