Automating repetitive tasks in Python

Even the most tenacious of us can be bored out of our brains by performing monotonous jobs. Fortunately, we live in the digital age, which provides many tools to relieve us of such tedious work.

While that capacity may appear to be dependent on our understanding of programming languages, we’re here to tell you that automation is for everyone, even if you’re a total beginner. Even if it seems intimidating at first, we guarantee that writing your first script will be rewarding, and your new skills will save you a lot of time in the long run.

Begin by considering the repetitious tasks that you perform daily and identifying those that you believe may be automated. Break down your workload into smaller sub-tasks and consider automating at least part of them.

After you’ve identified a suitable task, you’ll need to select the appropriate instrument. And that isn’t easy, not least because of the vast number of languages offered. With this post, we’ll try to persuade you that Python is the right choice for you because it’s simple to learn and has proven effective in various fields.

So, without further ado, let’s get started — and see why Python is a good choice for automation.

Why should you use Python to automate your tasks?

Python has a very readable and easy-to-understand syntax. Compared to other languages, Python is unquestionably one of the most straightforward. The latter has a direct English feel, making it an excellent place to start your adventure.

As we said before, Python’s advantages make the learning process quick and enjoyable. You may learn to develop simple scripts in a short amount of time and effort. Even for seasoned engineers, this smooth learning curve dramatically speeds up development.

Another factor that may persuade you to adopt Python is its excellent data structure support.

Python includes various types of data structures by default, including lists, dictionaries, tuples, and sets, which allow you to store and access data. These structures make data management efficient and straightforward, and when used appropriately, they can improve software performance. Furthermore, the information is preserved safely and consistently.

Even better, Python allows you to construct your data structures, making it a tremendously versatile language. While data structures may not appear to be very relevant to a newbie, believe us when we say that the deeper you go, the more critical your data structure choice becomes.

Python allows you to automate almost anything. From sending emails and filling up PDFs and CSVs (if you’re not familiar with this file format, it’s used by Excel, for example) to connecting with external APIs and performing HTTP requests, there’s a lot you can do with it. Whatever your concept is, Python and its modules and tools are more than likely to help you realize it.

Python’s libraries make the language extremely strong, allowing developers to tackle everything from machine learning and web scraping to operating system management.

Python also has a good support structure and a massive community of users. The language’s popularity continues to grow, and articles covering virtually all of the language’s concepts continue to appear on the web — a quick search is bound to turn up some exciting blog or StackOverflow posts. Alternatively, post your query to any Python forums.

You won’t be alone with your dilemma for long.

A vibrant community surrounds Python, and the language itself is constantly evolving. Plus, new third-party libraries appear regularly.

Python has found application in various professions and businesses, including science, data analysis, mathematics, networking, and more, despite not being a darling of the software development industry.

What can Python be used to automate?

Nearly anything!
You may automate almost any repetitious task with a bit of effort.

To do so, all you need is Python installed on your computer. We wrote all of the examples herein Python 3.9 and the relevant libraries. We’re not going to teach you Python; I’m just going to show you how easy it is to automate with it. We used iPython in the examples below, which is a tool that allows you to develop code interactively and step by step.

Python’s built-in libraries should be enough for basic automation. In other circumstances, we’ll tell you what needs to be installed.

File reading and writing

Reading and writing files is an activity that Python can help you automate quickly. First, all you need to know is where the files in your filesystem are located, their names, and which mode to open them in.

We used the statement to open a file in the example below, which is a method we highly suggest. Then, when the block code is complete, the file is automatically closed, and the cleanup is taken care of for us. More information is available in the official documentation.

Let’s use the open() method to load the file. The first input to open () is a file path, and the second is an opening mode. The file is loaded by default in read-only mode (‘r’).

with open(“text_file.txt”) as f:
  print(f.read())

Try the readlines() function to read the material line by line; it saves the data to a list.

with open(“text_file.txt”) as f:
  print(f.readlines())

The file’s contents are also changeable. However, this overwrites the original material! Loading it in write (‘w’) mode is one of the alternatives for doing so. The open () method’s second argument selects the mode.

with open("text_file.txt", "w") as f:
  f.write("Some content")
  
with open("text_file.txt") as f:
  print(f.read())

One excellent approach is to open the file in append (‘a’) mode, which means that the new material is appended to the end of the file while the previous content is preserved.

with open("text_file.txt", "a") as f:
  f.write("\nAnother line of content")
  
with open("text_file.txt") as f:
  print(f.read())

As you can see, Python makes reading and writing files a breeze. Feel free to learn more about the subject, particularly the file-opening modes, which are combined and extended! Combining writing to a file with Web scraping or interacting with APIs opens up a world of possibilities for automation! As a next step, you might want to look into an excellent library CSV that can help you read and write CSV files.

e-mail sending

Sending emails is another Python operation that you can easily automate. Python includes the fantastic smtplib library, which allows you to send emails via the Simple Mail Transfer Protocol (SMTP). Continue reading to learn how to use the library and Gmail’s SMTP server to send emails.

Naturally, you’ll need a Gmail account, and we strongly advise you to create a separate account for the sake of this script.

Why?

Because you’ll need to turn on the Allow less secure apps option, which makes it simpler for outsiders to access your personal information, set up your account today, and then let’s get started with the code. First and foremost, we must establish an SMTP connection.

import getpass
import smtplib
HOST = "smtp.gmail.com"
PORT = 465
username = "username@gmail.com"
password = getpass.getpass("Provide Gmail password: ")
Provide Gmail password:
server = smtplib.SMTP_SSL(HOST, PORT)

We use getpass to securely prompt the password and smtplib to create a connection and deliver emails, and we import the required built-in modules at the top of the program. The variables are set in the steps that follow. Gmail requires both HOST and PORT – they’re constants, which is why they’re written in caps.

Then you type in the password and your Gmail account name, which are saved in the username variable. It’s best to use the getpass module to enter the password. It asks for a password and does not repeat it back to you when you type it in. The script then uses the SMTP_SSL() function to establish a secure SMTP connection. The server variable holds the SMTP object.

server.login(username, password)
(235, b'2.7.0 Accepted')
server.sendmail(
"from@domain.com",
"to@domain.com",
"An email from Python!",
)
{}
server.quit()
(221, b'2.0.0 closing connection s1sm24313728ljc.3 – gsmtp')

Finally, you use the login() method to verify your identity, and that’s all! The sendmail() method will now be available for sending emails. Please remember to use the quit() method to clean up after yourself.

Scraping the internet

Web scraping is a technique for extracting data from Web pages and saving it to your computer. Assume that part of your job entails pulling data from a regularly visited website. Scraping could be highly effective in this situation since once code is developed, it can be executed multiple times, making it particularly useful when dealing with massive amounts of data.
Manually extracting data takes a long time and lots of clicking and searching.

Scraping data from the web has never been easier than using Python. However, before you can analyze and extract data from HTML code, you must first download the target website. The requests library will take care of everything – install it.

In your console, type the following:

pip install requests

Now that the page has been downloaded, we may extract the data we need. BeautifulSoup comes in handy here. The library aids in the parsing and extraction of information from structured files. The library must, of course, be installed first. Type the following in your console, just like before:

pip install beautifulsoup4

Let’s look at a simple example to learn how the automation feature works. The HTML code of a webpage we chose for processing is concisely understandable, considering its function is to display the current week of the year. Right-click anywhere on the page and select View page source to study the HTML code.

Then start the interactive Python by typing ipython in the console and begin fetching the page using requests:

import requests
response = requests.get("https://codeunderscored.com/")
response.status_code

The page is then downloaded and saved in a response variable after that. In the interactive terminal, type response.content to see the contents. The HTTP status (200) shows that the request is completed successfully.

It’s now up to BeautifulSoup to finish the job. We begin by importing the library and then constructing the soup BeautifulSoup object. The fetched data is used as an input to generate the soup object. We also tell the library which parser to use, which is html.parser for HTML pages.

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

The HTML file has now been saved to the soup object. It is shown as a nested structure (its fragment is printed above). There are a few different ways to get around the structure. The following are a handful of them.

soup.title
soup.title.string
soup.find_all("p")

You can quickly extract the page’s title or locate all of the content tags found in the data. The easiest way to acquire a sense of it is to play about with the thing.

When looking for data hidden in a table under the heading tag, we may use find to extract the table from the soup object and store it in a variable. It’s straightforward to get all the tags that store the information once the table has been saved. When you use find_all() on table_content, you’ll get a list of tags like . Iterate through the list and get_text() from each item to output them in a nice-looking format.

table = soup.find("table")
print(table)

table_content = table.find_all("td")
for tag in table_content:
print(tag.get_text())

We extracted exciting content from the page using only a few commands thanks to the beautiful BeautifulSoup package and simple steps. Libraries are great and beneficial when working with more significant, hierarchical HTML texts.

Interacting with a Programming Interface (API)

API interaction grants you superpowers! Let us explore this application’s example: pulling air quality data updates from the web.

Various APIs are accessible, but the Open AQ Platform API appears to be the most appealing because it does not require authentication (the relevant documentation can be found here: Open AQ Platform API ). The API returns air quality data for the provided location when queried. We used the requests library the same way we did in the previous example to get the data.

import requests
response = requests.get("https://api.openaq.org/v1/measurements?city=Paris&parameter=pm25")
response.status_code
response_json = response.json()

The code above retrieved Paris air quality statistics, focusing solely on the PM25 figure. You can personalize the search in any way you like; if you want to learn more about it, consult the API documentation.

The json() method on the response object is used. The script then saved the extracted data in key-value JSON format, which is more readable and cleaner. The sample response is shown below.

print(response_json)

The actual values drawn are concealed under the results key, with the most recent pulls near the head of the list, allowing us to extract the most recent value by reading the list’s first element with index zero. We can retrieve the PM25 concentration in the air in Paris on Feb 19, 2022, using the code below.

print(response_json["results"][0])

Data Compilation

It can take a long time to look through reports, PDFs, Excel spreadsheets, and other papers that contain information and data. Whether you need to extract data from a document or a webpage, you may use Python code to compile the information you need.

Python includes several modules that assist with data reading. You can develop a Python script to swiftly scan through a document and compile the data you need to extract no matter what sort of document or data source you’re dealing with. You may even use Python to generate the data you need in any format you wish.

How many hours have you spent traveling between papers and web pages while manually inputting information and formatting it to match your organization’s needs? If you learn Python, you can say goodbye to copying and pasting massive amounts of data.

Produce Reports

This duty is similar to data collection, but if you’ve ever had to prepare a regular report for coworkers or bosses, you know how time-consuming it can be. Creating reports entails more than simply data collection. You must put information into context. Your company’s preferred reporting format is likely to be the same.

Python can help you save time by compiling the data you need for your report and generating it. You can program your Python script to create reports on a specified timetable. The parameters you’d like to consider and your Python code will take care of the rest.

If you usually email your reports to the people who need to know, you may use your email automation to do so as well.
It is a real-world example of how Python automation may help a firm become more efficient by simplifying and expediting essential activities and workflow.

Python has the advantage that if the people who receive your reports are also Python users, they can automate the reading of the report and extract any actionable data. Imagine the efficiency of a company that is completely automated using Python.

Conclusion

Scripts written in Python are used to automate a variety of tasks. Humans or bots can execute Python scripts. Executing a task or sequence with little or no human intervention is called automation. A bot refers to a software code that automates the execution of Python scripts. Automation, especially for repeated processes, can save you time and boost productivity.

These are just a handful of Python’s time-consuming chores that can help you automate. Python’s automation capabilities and uses are, of course, nearly limitless. Python scripts are also used to automate web building and other more complicated activities.

You don’t need to be a software developer or have a lot of programming knowledge to construct sophisticated, time-saving automation scripts with Python. However, the more you understand Python automation, the more you will benefit from it.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *