4 Simple Python Ideas to Automate Your Workflow

4 Simple Python Ideas to Automate Your Workflow

August 9, 2019 Data Based Consulting programming 0
automating services

Automation. It’s supposed to remove work. So, why not help it along?

Truth be told, automating even simple tasks robustly takes time and a lot of dependency management which can become very complex.

Nevertheless, it can be very rewarding to automate some simple tasks. Even if it was more for fun.

For those that are Python fans already, you know how friendly Python is for interacting with just about anything. From sending HTTP requests, interacting with APIs, to loading and managing databases.

Python is a great choice for automation.

Here are five simple things you can automate that can help your various workflows and projects.

1. Moving the Mouse Automatically So Your Skype/Lynk Shows You As Active at Work

Imagine you have been scrolling through the newsfeed of your favorite social media app for the last ten minutes. Suddenly, your Lynk active symbol turns yellow. You’re inactive and not in a meeting.

Your nosey boss comes over and taps you on the shoulder.

Surprised, you look up to see him glaring down at you. He has a bad habit of micro-managing every second of your time.

“Hey, uh, I want you to relax and all but how is that module coming along,” he asks. You grit your teeth and provide some sort of polite answer.

“Cool, well, I just want to make sure you are focusing. Janice, our VP, is really expecting us to meet our deadline.”

End of interaction.

If only Lynk didn’t call you out like that.

Wait, you can get around this. After a few moments of Googling, you find a script to move your mouse and start coding up a job to run every few minutes that will make sure your mouse is moving.

These movements of the mouse and the keyboard can help make it look like you are active at work, in the case of Skype or Lynk.

The automation movement uses Python’s PyAutoGUI module. To install the PyAutoGUI module, run the following command:

pip install pyautogui

Python controls and tracks the mouse using your screen’s coordinate system. For instance, if you use a 1920×1080 screen resolution, the coordinate system for your screen would look like this:

automation consulting

The following functions are available under the PyAutoGUI module:

  • size(): The size() function lets you know your screen resolution.
import pyautogui
print(pyautogui.size())

Running this file (saved as .py extension), the Python code uses the size()function to give the x, y format of your screen resolution as output:

  • moveTo(): The moveTo() function in the PyAutoGUI module moves the mouse. The code below uses the function, which takes the coordinates of x and y, and an optional duration argument. The function uses a specified time duration argument to move your mouse pointer from a current position to the coordinates of x and y.
import pyautogui
print(pyautogui.size())pyautogui.moveTo(150,100, duration = 2)

Running this Python script magically moves your mouse pointer from its present position to coordinates (150, 100), taking 2 seconds to complete the process.

Other than some of these basics, you can also use functions, such as click and typewrite, for both click and type strings.

All of which can be used to help ensure that your boss doesn’t catch you slacking off any time soon.

2. Automating the Website Login Process Using Selenium

automating consulting

Many sites frown on using automated crawlers and programs to log in to their websites. But, it’s still a great skill to have.

Selenium is a useful library that can be used by multiple languages and that can help automate UI QA, or even to scrape websites with a login.

Although learning Selenium can take some time, start small. Just build a tool that can log in to your favorite site.

To get started, it’s essential that you install Chrome Driver and the Selenium library for Python. Use the command below to add the Selenium library.:

pip install Selenium

To start, we need to import some selected modules from Python’s Selenium library. Add the command below:

From selenium import 
webdriverdriver = webdriver.Chrome()

Now, to automate the login process, we start with a simple task like a login form for a website.

The task entails:

  • Navigating to the website.
  • Finding login fields (usually username and password).
  • Filling in your login details.
  • Submitting login details to get you through the login screen automatically.

Tip: You may decide to define a “method” (for frequent logins) to enable reuse in other tests.

The code lines would look similar to:

As you will notice, you need to get the elements you want to interact with. This is arguably one of the harder parts of creating a web-scraper or automated script that logs in to sites.

There are several ways to detect the elements of a web application to find the login fields.

Common ways of finding elements include ID (like the example above), CSS selectors, name, and XPaths.

A different approach to locating elements for the login process can be found in the official documentation of Selenium. Some websites engage more dynamic content (like several JavaScript!).

Overall, watching Selenium automatically click buttons and log in to your favorite sites is mesmerizing. Things just start moving and working without your interaction.

Almost like magic!

3. Automatic File Backup

Creating automatic backup files can be very useful if you perform regular backups.

You may be conversant with ZIP files (files with the .zip extension). ZIP files can contain several files with their compressed contents. And, as ZIP files can also hold many folders and subfolders, it becomes a handy way to backup files by packaging them into one.

A single ZIP file, called an archive file, can be created automatically using Python functions in the zipfile module.

You can also open (or extract) ZIP files using Python. The script is shared below.

This script was taken from the book Automate the Boring Stuff with Python(in case you want even more ideas).

4. Automatically Post YouTube Videos to Reddit Threads

Another one-off idea that we recently saw and thought was an interestingway of automating tasks you might do quite often, is using a script to post multiple videos to Reddit.

Posting YouTube videos in Reddit threads can also be automated. The use of PRAW, a Python wrapper that enables you to scrape data, can offer much more to your Reddit experience.

To get started, install PRAW using pip.

The script below automatically posts YouTube videos to Reddit threads.

Conclusion

Automating tasks can be a lot of fun. Watching your program automatically log in to your favorite site, or load hundreds of files into a database, can make you feel as if you have re-designed the wheel.

And, there are a lot of great libraries out there that can help you automate tasks easily. Excel reports, emails, and other tasks can be simplified in a few lines of code.

As long as it is maintainable!

If you enjoyed this video about software engineering then consider these videos as well!

Using Python To Scrape Meet-up API
The Advantages Healthcare Providers Have In Healthcare Analytics
142 Resources for Mastering Coding Interviews
Learning Data Science: Our Top 25 Data Science Courses
The Best And Only Python Tutorial You Will Ever Need To Watch
Dynamically Bulk Inserting CSV Data Into A SQL Server
4 Must Have Skills For Data Scientists
What Is A Data Scientist