Python automation: Useful Scripts Which Can Help You to Automate Critical Workflow

Why Use Python for Automation?

Automation means getting computers to do repetitive tasks for you, freeing up your time for more important things. Python is great for automation because it’s easy to understand, like reading English rather than a complicated foreign language. It also has many ways to handle data, making it simple to manage information for your automated tasks.

Python comes with a wide range of libraries that let you automate almost anything you can think of, from managing files to machine learning. It’s also got a massive community, so if you run into problems, you can usually find help online. Overall, Python’s simplicity, data-handling capabilities, and extensive libraries make it a top choice for automation.

Programming languages like Python can do many things, but learning all of them can be overwhelming. I’ve put together nine ideas for using Python to automate tasks. These ideas can help simplify tasks that might otherwise take up a lot of your time, like interacting with APIs, scraping information from websites, reformatting data, and more. To use these automation ideas, you may need to install some libraries. Just follow the instructions, and you’ll soon have Python doing tasks for you automatically.

  1. Pulling live traffic(Google Map) data

for this you can use requests library in Python

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

# Replace 'YOUR_API_KEY' with your actual Google Maps API key
API_KEY = 'YOUR_API_KEY'

# Define the location for which you want to fetch traffic data
location = 'Mumbai'

# Make a request to the Google Maps API
response = requests.get(f'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={location}&destinations={location}&departure_time=now&key={API_KEY}')

# Check if the request was successful
if response.status_code == 200:
data = response.json()
duration_in_traffic = data['rows'][0]['elements'][0]['duration_in_traffic']['text']
print(f"Estimated travel time in current traffic from {location}: {duration_in_traffic}")
else:
print("Failed to fetch traffic data")


2. Gathering data from webpages (Web Scrapping):

Gathering data from webpages is a common task that Python can streamline. Python offers powerful web scraping tools like BeautifulSoup. To begin, you’ll need to download the page using Python’s requests library. If you haven’t installed BeautifulSoup yet, you can do so with the following command:

pip install beautifulsoup4

After installing the necessary tools, we can utilize the requests library to fetch the content of the desired web page. For example, if you aim to extract today’s headlines from the BBC News Home page, you can use the following script to download its content.

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

If Python returns a status code of 200, it indicates that the page’s contents were successfully downloaded.

While you could view all of the page’s content by simply typing response.content, it’s more practical to extract specific elements from the page instead of dealing with a large HTML block. This is where BeautifulSoup becomes useful.

To achieve this, import BeautifulSoup and create an object named soup, which parses HTML pages (since BBC uses HTML), using the following commands:

from bs4 import BeautifulSoup                          
soup = BeautifulSoup(response.content, 'html.parser')
print(soup)

Now that the page is saved in your soup element, you can extract the data you need from it. BeautifulSoup provides various commands to help you pull specific types of data. For instance, if you wish to extract all the H2 news headings from the site, you can use the following command:

print(soup.find_all('h2'))

the html format you see in the output above can be copied and a web page can be created out of it automatically.

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

P.S: Make sure you stick to rules while performing web scrapping.

3. Convert PDF to audio file

For individuals with visual impairments or those who prefer listening to audiobooks, Python provides libraries that simplify text-to-speech conversion. In this script, we’ll utilize PyPDF to extract text from PDFs and Pyttsx3 to convert that text into speech.

The following script processes a PDF, eliminates unnecessary spaces and lines that could disrupt the natural flow of the text when read aloud, and then converts the PDF into an audio file. You could also apply PyPDF and Pyttsx3 to convert a webpage into an audio file if you had a web scraping script in place.

import pyttsx3
import PyPDF2

# Replace 'file.pdf' with the path to your PDF file (e.g., '/Desktop/Contracts/file.pdf')
pdfreader = PyPDF2.PdfReader('C:/Users/Swarna Khushbu/Desktop/Coding/Certification.pdf')

reader = pyttsx3.init()

# Iterate over each page in the PDF
for page_num in range(len(pdfreader.pages)):
page = pdfreader.pages[page_num]
text = page.extract_text()

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

reader.say(legible_text)

# Save each page's text to a separate audio file
reader.save_to_file(legible_text, f'page_{page_num + 1}.mp3')
reader.runAndWait()

reader.stop()

Explanation of above code:

  1. import pyttsx3: This line imports the pyttsx3 module, which is a text-to-speech conversion library in Python.
  2. import PyPDF2: This line imports the PyPDF2 module, which is used for reading PDF files in Python.
  3. pdfreader = PyPDF2.PdfReader('C:/Users/Swarna Khushbu/Desktop/Coding/Certification.pdf'): This line creates a PdfReader object pdfreader by opening the specified PDF file (‘Certification.pdf’ in this case).
  4. reader = pyttsx3.init(): This line initializes the pyttsx3 engine reader for text-to-speech conversion.
  5. for page_num in range(len(pdfreader.pages)):: This line starts a loop that iterates over each page in the PDF file.
  6. page = pdfreader.pages[page_num]: This line retrieves the current page object from the PdfReader object based on the current page_num.
  7. text = page.extract_text(): This line extracts the text content from the current page and stores it in the variable text.
  8. legible_text = text.strip().replace('\n', ' '): This line cleans up the extracted text by removing leading and trailing whitespaces and replacing newline characters with spaces.
  9. print(legible_text): This line prints the cleaned-up text to the console.
  10. reader.say(legible_text): This line instructs the pyttsx3 engine to convert the cleaned-up text to speech.
  11. reader.save_to_file(legible_text, f'page_{page_num + 1}.mp3'): This line saves the converted speech to an MP3 file, where each page’s speech is saved to a separate file with a name like page_1.mp3, page_2.mp3, etc.
  12. reader.runAndWait(): This line ensures that the speech is played without any interruption.
  13. reader.stop(): This line stops the pyttsx3 engine once all pages have been processed.

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:

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)

Explanation of above code:

  1. import os, sys: Imports the os and sys modules, which provide functions for interacting with the operating system and Python runtime environment, respectively.
  2. from PIL import Image: Imports the Image class from the PIL module, which is used for working with images in Python.
  3. images = ['test.png']: Creates a list called images containing the filename 'test.png'. This is the image file that will be converted.
  4. for infile in images:: Iterates over each filename in the images list.
  5. f, e = os.path.splitext(infile): Uses the os.path.splitext() function to split the filename infile into its base name (f) and extension (e). This is used to create the output filename.
  6. outfile = f + '.jpg': Creates the output filename by appending the ‘.jpg’ extension to the base name f.
  7. if infile != outfile:: Checks if the input filename is different from the output filename. This is necessary to avoid overwriting the original image.
  8. try:: Starts a try-except block to handle exceptions that may occur during the image conversion process.
  9. with Image.open(infile) as image:: Opens the input image file using the Image.open() method and assigns it to the image variable. The with statement ensures that the image file is properly closed after use.
  10. in_rgb = image.convert('RGB'): Converts the input image to RGB format using the convert() method. This is necessary because JPEG format does not support all the color modes that PNG format does.
  11. in_rgb.save(outfile, 'JPEG'): Saves the converted image to the output file using the save() method. The second argument specifies the format (‘JPEG’) to save the image in.
  12. except OSError:: Catches any OSError exceptions that occur during the image conversion process.
  13. print('Conversion failed for', infile): Prints a message indicating that the conversion failed for the current input file.

This script converts a PNG image ('test.png') to JPEG format ('test.jpg') in the root directory. It uses the PIL module’s Image class to open, convert, and save the image.

5. Bulk uploading files to a cloud-based platform

Uploading a single file to the cloud may not be a big hassle, but uploading many files manually can be time-consuming. Fortunately, Python’s requests library can help automate this task. In the example below, we’ll focus on uploading files to Google Drive. To enable Google’s authentication process, you’ll need to set up a project and credentials in your Google Cloud Dashboard. Here’s a brief guide on 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()

6. Clean up your computer

If your computer desktop is cluttered, Python can be a lifesaver for organizing your files quickly. Python’s OS and shutil modules are perfect for this task, enabling you to rename files, create folders, and more. The script below demonstrates a simple example: creating a folder called “Everything” on your desktop and moving all files into it. You can explore further customization by experimenting with these modules.

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)

P.S: Don’t hesitate to run the code nothing will happen to you laptop/desktop i run it once at least a month.

Explanation of Above Code:

  1. import os, shutil: Importing the os and shutil modules, which provide functions for interacting with the operating system and file system.
  2. lis=[]: Creating an empty list called lis.
  3. i=1: Initializing a variable i with the value 1.
  4. destinationdir='/Users/NAME/Desktop/Everything': Setting the initial destination directory path where the files will be moved.
  5. while os.path.exists(destinationdir):: Starting a while loop that continues as long as the destinationdir already exists.
  6. destinationdir+=str(i): Appending the current value of i to the destinationdir string.
  7. i+=1: Incrementing the value of i by 1 for the next iteration.
  8. os.makedirs(destinationdir): Creating the destination directory using the os.makedirs() function.
  9. lis=os.listdir('/Users/NAME/Desktop'): Listing all files and directories in the specified directory (/Users/NAME/Desktop) and storing them in the lis list.
  10. for x in lis:: Starting a for loop to iterate over each item (x) in the lis list.
  11. print x: Printing the current item (x) in the loop.
  12. if x==__file__:: Checking if the current item (x) is the script file (__file__ represents the current script file).
  13. continue: If the current item is the script file, the loop continues to the next iteration without executing the following code.
  14. shutil.move(x,destinationdir): Moving the file (x) to the destination directory (destinationdir) using the shutil.move() function.

This code essentially moves all files from the desktop to a new directory called “Everything” (or “Everything1”, “Everything2”, etc., if “Everything” already exists) on the desktop, except for the script file itself.

Conclusion:

Python automation offers a wide range of possibilities to streamline and simplify critical workflows. With Python’s rich set of libraries and tools, users can automate tasks that would otherwise be time-consuming and error-prone.

One key aspect of Python automation is web scraping. Python’s libraries, such as BeautifulSoup and Scrapy, allow users to extract data from websites quickly and efficiently. This can be particularly useful for tasks such as gathering information for analysis or monitoring competitor websites.

Another important use case for Python automation is file management. Python’s os and shutil modules provide functionalities for file operations such as renaming, moving, and deleting files. This can be handy for organizing files and folders, especially when dealing with large amounts of data.

Python automation can also be applied to tasks such as data processing and analysis. Libraries like pandas and NumPy enable users to perform complex data manipulations and calculations with ease. This can be beneficial for tasks such as data cleaning, transformation, and analysis.

Overall, Python automation offers a flexible and powerful solution for automating critical workflows. By leveraging Python’s capabilities, users can save time, reduce manual errors, and focus on more important aspects of their work.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>