Automation is the way of the future (and the present). A single line of automation code can trigger an infinite number of actions — adding millions of data points to a CSV file, renaming thousands of files, or scraping the web for powerful information. Beyond simplifying tasks, automation reduces the risk of human error and significantly boosts efficiency. Python, renowned for its automation capabilities, offers a variety of built-in methods and functions, alongside a rich ecosystem of libraries and tools, all designed to streamline and enhance task automation.
What can Python automate and what libraries are involved?
File Handling
Automated file handling removes the need for a developer to manually rename, move, or organize files. Automating file handling saves time and is useful in managing logs, reports, and backups efficiently.
The os
module (native to Python) provides functions to interact with the operating system, such as handling files and directories.
In the code example below, the automation function being used is rename()
. rename()
comes from the os
module and is written as os.rename(old_path, new_path)
. This automatically renames each file in a folder with a given prefix and number. The rest of the code loops through all files in the folder and applies rename()
to each one.
View the code on Gist.
Data Entry (Excel or CSV)
Manually entering data into spreadsheets is time-consuming and error-prone. Automating this process ensures accuracy and saves valuable time.
pandas
is a data analysis library that provides powerful tools for handling structured data, like CSV and Excel files. pandas
is not native to Python and must be installed.
The method performing automation in this example is to_csv()
, which is written as df.to_csv(file_path, index=False)
. This method automatically writes a modified DataFrame to a CSV file, eliminating the need for manual editing. The rest of the code reads an existing CSV, adds a column, and then writes the updated data back to the file.
View the code on Gist.
Automated Emails
I don’t know anyone who wants to send another email. Manually sending emails can be tedious, and nearly impossible for recurring reports or notifications. Automating emails improves efficiency and eliminates that item from your to-do list.
The smtplib
module allows Python to send emails using SMTP (Simple Mail Transfer Protocol).
The method that performs the automation in this example is sendmail()
, written as server.sendmail(sender_email, to_email, msg.as_string())
. This automatically sends an email without human intervention. The remainder of code in the example body creates the email message and connects to the mail server.
View the code on Gist.
Web Scraping
I’m not sure what manual web scraping would look like. Luckily we don’t have to worry about that since we can automate. Automating web scraping allows users to collect large amounts of data quickly.
BeautifulSoup
is a web scraping library that extracts data from HTML and XML documents. BeautifulSoup
is not part of native Python and must be installed.
The function automating the task here is soup.find_all()
, written as soup.find_all("h2")
. This extracts all
headings from a webpage, eliminating the need to manually copy them. The other code included in this example loads the webpage’s content and parses it for the desired data.
View the code on Gist.
Automating Reports (PDF Generation)
Manual report generation could lead to inconsistent results. It’s much easier to approach report generation in a set it and forget it manner. Not only does it make the task much easier but it also ensures consistency and efficiency.
reportlab
allows Python to generate PDFs programmatically. reportlab
is not native to Python and must be installed.
The method that automates the process here is canvas.drawString()
, written as c.drawString(100, 750, text)
. This places text onto a PDF automatically. The reminder of the code builds a new PDF file and saves it.
View the code on Gist.
Final Thoughts
Automation is a game-changer. In today’s fast-paced world, where multitasking is the norm, eliminating even a single task from your to-do list is a win. With just a few lines of code, you can not only simplify a task but also execute it faster and with fewer errors. Python offers built-in functionality and is supported by a vast ecosystem of third-party tools, making automation easier than ever.
The post Python’s Automation Magic appeared first on The New Stack.