Loading
Loading
  • Home

  • Productivity

  • App tips

App tips

9 min read

Python automation: 9 scripts to automate critical workflows

By Luke Strauss · December 21, 2022
Hero image with the Python logo

I don't know about you, but the highlight of my day is completing mind-numbing and repetitive tasks—manual data entry, downloading hundreds of images one-by-one, and searching every file on my desktop for the right document. 

Can't relate? Weird. But lucky for you, we live in a time where computers can help with the busywork we've become so accustomed to doing by hand. Python is a particular favorite programming language among those new to task automation.

I consulted with a Python expert to develop nine Python automation scripts, each of which can automate tasks you've been doing manually for way too long.

Related reading: Not sure when to automate a task? Start here.

Why automate tasks with Python?

Automation lets you hand off business-critical tasks to the robots, so you can focus on the most important items on your to-do list—the ones that require active thought and engagement. No-code automation should be your first stop, but Python's popularity for task automation comes from a variety of factors, including its:

  • Simplicity and intuitiveness: Compared to many other programming languages, Python is very easy to read and comprehend. While learning a language like C++ or Java can feel like learning a foreign language, Python syntax resembles English. 

  • Support of data structures: Python offers several ways to store data—including lists and dictionaries—and the ability to create your own data structures. This makes data management easy, improving automation responsiveness.

  • Extensive automation capabilities: Python comes equipped with a huge set of libraries that enable you to accomplish nearly any automation goal that comes to mind—machine learning, operating system management, and more. Plus, Python's support network is huge, so you should be able to find an answer to nearly any automation question online.

How to run a Python script

Before you start creating scripts, here's a little refresher on how to use Python. First, download Python onto your device (for free!). Once you download it, you can create and run a script.

Your script file needs to be named with the extension .py, which stands for "Python." This tells your device that the file will contain Python code. You'll then add your script to this file and run it using your device's command-line or terminal.

After creating your file and opening your terminal, type python3 followed by the path to your script. For example, on my Mac, I would:

  1. Create an empty text file called FILE.py.

  2. Add my script

  3. Open the Terminal app

  4. Type python3

  5. Drag the file into the app, and press enter, which would run the script.

I created a simple script as an example. Here's what the file looks like:

And here's what it looks like to execute the script in Terminal:

If you're running one of the most recent versions of Windows, you should be able to type your .py file's name in the Windows Command Prompt and run it without first typing the python3 command. And voilá—you're ready to get automating.

9 useful Python automation script examples

Uses of programming languages are practically unlimited, but nobody has time to learn every script there is. I've compiled nine Python automation ideas to simplify tasks that may otherwise distract you from important work, grouped into the following categories:

  • Interacting with APIs

  • Web scraping

  • Reformatting

  • Reading and writing files

  • Consolidating tasks

  • Organizing data

Running these scripts may require some preparatory steps, like downloading libraries. Follow the directions below, and you'll have Python completing tasks for you in no time.

1. Pull live traffic data

APIs make it possible to retrieve real-time data from third parties. Traffic data is always changing, so it presents a great opportunity to work with an API. With Python, we can quickly pull live traffic data as long as we have the URL where the API collects the data. For this example, I used TomTom's API URL that enables access to live Los Angeles traffic data.

In the following script, I first install the requests library in order to gather data from the URL, then unpack the URL as a JSON file.

Requests library:

pip or pip3 install requests

# Which one you use (pip or pip3) depends on the version of Python and the package manager pip you're using

import requests

url_api = 'https://api.midway.tomtom.com/ranking/liveHourly/USA_los-angeles'

usa_req = requests.get(url_api)

usa_json = usa_req.json()

# Do something with the json response to prove it works. 

print(usa_json)

Automation goal: Interacting with APIs

2. Compile data from a webpage

If your workday involves regularly pulling fresh data from the same websites, Python's web scraping capabilities can save you a lot of time. While it has specialized libraries to extract from specific sources like Wikipedia, the following script uses a more versatile web parsing and scraping library called BeautifulSoup.

Before you can extract a page's data, you first have to download it with Python's requests library. Refer to the command from the script above to download the requests library if you haven't yet, and the command below to install the BeautifulSoup library.

BeautifulSoup library:

pip or pip3 install beautifulsoup4

With all the tools we need installed, we can use the requests library to download the contents of the page of interest. Let's say you wanted to pull today's headlines from the BBC News Home page. Here's a script to download its contents.

import requests

response = requests.get('https://www.bbc.com/news')

print(response.status_code)

If Python outputs the status code 200, that means it successfully downloaded the page's contents. Theoretically, at this point, you could just type response.content to see all of the page's contents. But, chances are, you want to see specific elements on the page rather than a big wall of HTML, which is where BeautifulSoup comes in.

Import BeautifulSoup and create an object called soup that parses HTML pages (since BBC uses HTML) using the following commands:

from bs4 import BeautifulSoup                          

soup = BeautifulSoup(response.content, 'html.parser') 

print(soup)

The page is now saved in your soup element, and you can extract whatever data you need from it. There are a lot of commands that enable you to pull specific types of data. For example, if you wanted to pull all of the H2 news headings from the site, you could input:

print(soup.find_all('h2'))

Play around with the BeautifulSoup library to pull whatever data you need from a webpage with the punch of a command.

Automation goal: Web scraping

3. Convert PDF to audio file

For the visually impaired (or for those of us who would rather listen to an audiobook than pick up a physical copy), Python offers libraries that make text-to-speech a breeze. For this script, we'll be pulling from PyPDF—a library that can read text from PDFs—and Pyttsx3, which converts text to speech.

The below script takes a PDF, removes spaces and lines that would impede Pyttsx3's ability to read the text naturally, and converts the PDF to an audio file. If you were to implement some web scraping script, you could also use PyPDF and Pyttsx3 to convert a webpage into an audio file.

import pyttsx3,PyPDF2

#Replace file.pdf' with path to your PDF file (i.e. /Desktop/Contracts/file.pdf)

pdfreader = PyPDF2.PdfFileReader(open('file.pdf','rb'))

reader = pyttsx3.init()

for page in range(pdfreader.numPages):   

    text = pdfreader.getPage(page).extractText()

    legible_text = text.strip().replace('\n',' ')

    print(legible_text)

    reader.say(legible_text)

    reader.save_to_file(legible_text,'file.mp3')

    reader.runAndWait()

reader.stop()

Automation goal: Reformatting

4. Convert a JPG to a PNG

Let's say you want to improve a JPG image's capacity for high color quality and clarity by converting it to a PNG. With Python, changing the format of an image is simple. First, install the PIL package with the following command:

pip or pip3 install Pillow

Next, run the following script, filling in the placeholder path next to Image.open with the path to the image you want to modify, and the placeholder path next to im1.save with the path of the location you want to save your new PNG.

import os, sys

from PIL import Image

# Make sure original image ('test.png') is in root directory

images = ['test.png']

for infile in images:

    f, e = os.path.splitext(infile)

    outfile = f + '.jpg'

    if infile != outfile:

        try:

            with Image.open(infile) as image:

                in_rgb = image.convert('RGB')

                in_rgb.save(outfile, 'JPEG')

        except OSError:

            print('Conversion failed for', infile)

Automation goal: Reformatting

5. Read a CSV

CSV (Comma Separated Values) is a common format for importing and exporting spreadsheets from programs like Excel. Python can read a CSV, meaning that it can copy and store its contents. Once Python reads the spreadsheet's contents, you can pull from these contents and use them to complete other tasks.

import csv

#replace 'customers.csv' with your filename or path to file (i.e. /Desktop/folder/file.csv)

with open('customers.csv', newline='') as csvfile:

    cust_reader = csv.reader(csvfile, delimiter=' ', quotechar='|')

    for row in cust_reader:

        print(', '.join(row))

The above script would read and print out the contents of the CSV file "customers.csv," adding commas between them.

Automation goal: Reading and writing files

6. Modify a CSV

You can also modify an existing CSV file using Python's write feature. This replaces the contents in a CSV file without needing to enter new data manually. Learn more about how to read and write CSV files using Python's CSV library.

import csv

Row = ['James Smith', 'james@smith.com', 200000]

# Make sure 'customers.csv' is in your root directory, or provide path to open() method.

# pass in 'a' to append new row or 'w' to overwrite CSV file

with open('customers.csv', 'a', newline='') as csvfile:

    cust_writer = csv.writer(csvfile, delimiter=',',

                            quotechar='|', quoting=csv.QUOTE_MINIMAL)

    cust_writer.writerow(Row)

Automation goal: Reading and writing files

7. Sending personalized emails to multiple people

Nobody likes sending 30+ nearly identical emails, one-by-one. If you work in a field that requires this—marketing, education, or management, to name a few—Python makes this significantly easier. Start by creating a new CSV and filling it with all of your recipients' information, then run the script below.

You may have to create an app password for Gmail to run the script. If you notice the script doesn't run without one, create your password and enter it following the prompt Enter password: in the script.

import csv, smtplib, ssl

from datetime import date

today = date.today().strftime('%B %d, %Y')

message = '''Subject: Your evaluation

Hi {name}, the date of your Q1 evaluation is {date}. Your score is: {score}'''

from_address = 'YOUR EMAIL ADDRESS'

# You may need to create an app password for Gmail here: https://myaccount.google.com/apppasswords

password = input('Enter password: ')

context = ssl.create_default_context()

with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as server:

    server.login(from_address, password)

    # use a customers.csv file with name, email, and score columns

    with open('customers.csv') as file:

        reader = csv.reader(file)

        for name, email, score in reader:

            server.sendmail(

                from_address,

                email,

                message.format(name=name,date=today, score=score),

            )

Automation goal: Consolidating tasks

8. Bulk uploading files to a cloud-based platform

While uploading a single file to the cloud likely isn't causing you a whole lot of grief, manually uploading tens or hundreds of files at a time can eat away at valuable time. Luckily, Python's requests library (which we've pulled from in earlier scripts) can simplify this process.

In the example below, we'll upload files to Google Drive specifically. In order for Google's authentication process to work, you'll need to set up a project and credentials in your Google Cloud Dashboard. Here's a quick overview of how to do this:

  1. In an existing or new Google Cloud Project, navigate to Library > Search "Google Drive API," and click Enable.

  2. Navigate to Credentials > + Create Credentials > OAuth client ID

  3. Create a consent screen by providing the required fields. 

  4. Repeat step two, this time selecting Application Type of Desktop App, and click Create.

  5. Download the auth JSON file, and place it in the root of the project alongside the .py script below. 

pip or pip3 install pydrive

from pydrive.auth import GoogleAuth

from pydrive.drive import GoogleDrive

google_auth = GoogleAuth()

drive_app = GoogleDrive(google_auth)

# Make sure files are in your root directory

upload_list = ['test.png', 'test.jpg']

for file_to_upload in upload_list:

  # Navigate to your desired Google Drive folder and grab custom ID from URL path

file = drive_app.CreateFile({'parents': [{'id': 'FOLDER_ID_FROM_GOOGLE_DRIVE'}]})

file.SetContentFile(file_to_upload)

file.Upload()

Automation goal: Consolidating tasks

9. Cleaning up your computer

If you're like me and your computer desktop looks like a warzone, Python can help you organize your life in a matter of seconds. We'll be using Python's OS and shutil modules for this process, as they allow Python to make changes to your device's operating system—think renaming files, creating folders, etc.—and organize files.

The below script creates a folder titled "Everything" on your desktop and moves all of the files into it. We're keeping it simple here, but feel free to play around with Python's OS and shutil modules to better organize your files.

import os, shutil

lis=[]

i=1

destinationdir='/Users/NAME/Desktop/Everything'

while os.path.exists(destinationdir):

    destinationdir+=str(i)

    i+=1

os.makedirs(destinationdir)

lis=os.listdir('/Users/NAME/Desktop')

for x in lis:

    print x

    if x==__file__:

        continue

    shutil.move(x,destinationdir)

Automation goal: Organizing data

Taking automation to the next level with Zapier

Zapier is equipped to automate a lot of processes—probably more than you know exist. But for those that aren't directly built into Zapier, you can use Code by Zapier to create triggers and actions using JavaScript or Python. 

It's also very likely you can automate your workflows without any code at all. Zapier's no-code platform lets you automate your business-critical work with a visual builder—no code required.

Related reading: 

Get productivity tips delivered straight to your inbox

We’ll email you 1-3 times per week—and never share your information.

tags

Related articles

Improve your productivity automatically. Use Zapier to get your apps working together.

Sign up
A Zap with the trigger 'When I get a new lead from Facebook,' and the action 'Notify my team in Slack'