Python is a popular high-level, interpreted programming language for beginners (as well as experienced programmers). It is used for various applications, including automation, data analysis, and web development. It is well renowned for being readable and having a straightforward syntax, making it simple to learn and use. Given that it is an open-source language with an engaged community, several algorithms, python libraries, modules, frameworks, and tools are available.
Regarding automation, Python offers many pre-built features that simplify and facilitate automation processes, like data handling, data manipulation, and even working on regular expressions. In addition, many third-party libraries can automate specific tasks, such as web scraping, email handling, or system administration.
In this blog, we are going to look at five Python automation applications for your boring tasks. But before that, let us look at why Python is a convenient programming language for automation, and what kind of tasks you can automate.
[If you are a Python beginner and always look for a “learning by doing” approach to learn things, I would highly recommend “Python Crash Course: A Hands-On, Project-Based Introduction to Programming” by Eric Matthes. You’ll find amazing small projects that will help you comprehend the topics more clearly. You can also download a free sample to have a peek inside the book.]
Why is Python best suited for Automation?
Python is a preferred programming language by developers for intelligent automation and hyper-automation because it is scalable, faster, offers 24/7 support, and has cross-platform accessibility. Well, these are just the initial points of discussion; in the succeeding blog, we will understand more deeply.
Data structures enable you to store and access data, and Python offers many types by default, including lists, dictionaries, tuples, and sets. These structures let you manage data easily and efficiently and increase software performance when chosen correctly. Furthermore, the data is stored securely and consistently.
Even better, Python lets you create your data structures, which, in turn, makes the language very flexible. While data structures may not seem all that important to a newcomer, trust me on this — the deeper you go, the more important your choice of data structure tends to become.
What can you automate with Python?
Python is a versatile and widely used programming language that almost all programmers are expected to know in the software development domain. Due to its versatility and straightforward syntax, this language is utilized for several automation tasks, including:
- Data Processing: Data processing is automatic data processing and analysis, including data transformation, cleaning, and visualization.
- Web Scraping: Web scraping is the automated collection of information from websites, such as news articles, product prices, or job listings.
- Machine Learning: Python may be used to automate operations related to machine learning, including developing and deploying machine learning models and automating the data preparation process.
- Software Testing: Python is capable of automating software testing, including the execution of automated tests, the creation of test reports, and the analysis of test findings.
- System Administration: Python can automate processes like initiating and terminating services, checking the health of servers, or scheduling backups.
So now that we have understood the fundamentals, let’s dive directly into practical applications.
Five tasks to automate using Python
1. Send E-Mail Using Python
A few weeks ago, I used a GPS sensor to get my location coordinates using a Raspberry Pi. [It’s just a few lines of Python code. Have a look]:
I had originally thought of this project to track and locate my bike and send me email alerts in case of a bicycle theft. Python offers a library for such use cases called smtplib
. It creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email ID on the internet. The Port number used here is ‘587’. Below is the code format that I used for my application. You can use the same for your application and type in your message
that you want to be sent.
# start TLS for security
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
# Authentication
s.login("sender_email_id", "sender_email_id_password")
#message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("sender_email_id", "receiver_email_id",message)
# terminating the session
s.quit()
2. Converting a PDF Text File to an Audio File (Text to Speech)
Sometimes, you have some important PDFs to read but just have enough time. Plus you look at the total number of pages and get overwhelmed already. Or you have the PDF of your favorite novel but do not like reading. Well, Python has the solution to all these difficulties.
Python has packages and modules that make text-to-speech simple for those of us who would prefer to listen to an audiobook than pick up a paper copy, or who would prefer listening to documents while commuting. For this script, you use PyPDF
, a library that can read text from PDF files, and Pyttsx3, a program that turns text into speech. You can also convert a website into an audio file using these libraries. Specifically, Pyttsx3 in Python 3 takes a PDF input, removes the spaces, and converts the remaining text to audio.
Here’s a brief information on the packages used for this application:
pyttsx3: It is a Python library for Text-to-speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to us
PyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit. It is capable of extracting document information, splitting documents page by page, merging documents page by page, etc.
In your virtual environment, you can install both these modules as follows:
pip3 install pyttsx3
pip3 install PyPDF2
Now let’s code.
# importing the modules
import PyPDF2
import pyttsx3
# specify path of the PDF file
path = open('file.pdf', 'rb')
# creating a PdfFileReader object
pdfReader = PyPDF2.PdfFileReader (path)
# the page with which you want to start
# this will read the page of 25th page.
from_page = pdfReader.getPage(24)
# extracting the text from the PDF
text = from_page.extractText()
# reading the text
speak = pyttsx3.init()
speak.say(text)
speak.runAndWait()
3. Data Compilation
Fetching data manually out of any report, PDF, or document would be a tedious task. Besides, there are chances of errors as well. And it wouldn’t be a feasible solution to spend an entire day or two on tasks that hardly require minutes.
Using Python modules, you can fetch and compile data of any sort. Besides, It allows you to write Python scripts that assist in fetching data of whatever size and type from the documents of your choice. Moreover, you can use Python to download the extracted wealth of information in your preferred file format.
import pandas as pd
# Read CSV files and concatenate them
# Place your CSV file name
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df_combined = pd.concat([df1, df2])
# Group data and calculate statistics
grouped = df_combined.groupby('category')
stats = grouped.mean()
# Save results to a new CSV file
stats.to_csv('results.csv')
4. Convert a JPG image to a PNG image
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 in your virtual environment with the following command:
pip3 install Pillow
Now let’s look at the code.
from PIL import Image # Adding the GUI interface
# To convert the image From JPG to PNG {Syntax}
img = Image.open("Image.jpg")
img.save("Image.png")
# To convert the Image From PNG to JPG img = Image.open("Image.png")
img.save("Image.jpg")
5. Download an image using a URL in Python
Downloading content from its URL is a common task that Web Scrapers or online trackers perform. These URLs or Uniform Resource Locators can contain the web address (or local address) of a webpage, website, image, text document, container files, and many other online resources. It is quite easy to download and store content from files on the internet. For this application, we will require the requests
library:
pip3 install requests
Now let us write some code.
import requests
from PIL import Image
url = 'https://-URL OF THE IMAGE'
# This statement requests the resource at
# the given link, extracts its contents
# and saves it in a variable
data = requests.get(url).content
# Opening a new file named img with extension .jpg
# This file would store the data of the image file
f = open('img.jpg','wb')
# Storing the image data inside the data variable to the file
f.write(data)
f.close()
# Opening the saved image and displaying it
img = Image.open('img.jpg')
img.show()
These are some of the five amazing Python applications to automate your work. But there’s more! Have you developed a Python application that you are proud of and what to make it public? Or maybe you want to deploy your version of one of the above applications? I’ve got you covered. You can directly head over the blog on deploying Python application using Docker and easily make your amazing Python application available to the world.
Conclusion
Python is a well-known programming language for automation because of its ease of use, adaptability, and extensive library system. It offers many benefits:
- A beginner-friendly syntax
- Easy integration capabilities
- Custom scripts, and much more.
Consequently, many operations can be automated, from direct file downloads to sophisticated data analysis, testing, decision-making, creating chatbots, and machine learning.
I would like you to go ahead and customize one of the above projects for your own application. You can post a video of your project on Instagram and tag me @machinelearningsite in that. Looking forward to your projects. Happy coding! 🙂
If you found this blog helpful, you can support me by following me on social media via the links provided below. I not only post content on Python but also on machine learning and cool projects on Raspberry Pi. Go ahead and check it out:
It may happen that you may miss some of the blogs I publish and thereby miss out on interesting projects and topics. To stay updated and get notified on those missed posts, join my newsletter: