13 Powerful Python Snippets To Automate Tasks
Tags: python, programming, beginners, tutorial
Published: 2023-06-06 04:09:16

Python, a versatile programming language, provides various functionalities and libraries to automate tasks effectively.

In this article, we will explore 13 advanced Python snippets that can automate cool and useful tasks, enabling you to save valuable time and effort.

From automating Google search results to retrieving Wi-Fi passwords, downloading YouTube videos, and more, these code snippets will empower you to accomplish tasks seamlessly. Let's dive into the world of automation with Python!

####Automating Google Search Results
```python
from googlesearch import search

query = "Medium.com"

for result in search(query, num=10, stop=10, pause=2):
    print(result)
```
This snippet utilizes the googlesearch library to fetch Google search results. Provide a query, and the snippet will return the top search results.

####Speed Testing with Python
```python
from speedtest import Speedtest

test = Speedtest()

# Download Speed
print(test.download())

# Upload Speed
print(test.upload())

# Ping test
server_names = []
test.get_servers(server_names)
print(test.results.ping)
```
Using the speedtest library, this snippet measures the download speed, upload speed, and ping of your internet connection.

####Sending Emails with Attachments
```python
import smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sender_email = 'sender@gmail.com'
password = 'xxxxxxxx'
receiver_address = 'receiver@gmail.com'
body = "This is a test Python email"

message = MIMEMultipart()
message["From"] = "sender@gmail.com"
message["To"] = "receiver@gmail.com"
message["Subject"] = "Python Mail"
message["Bcc"] = "receiver@gmail.com"

message.attach(MIMEText(body, "plain"))

filename = "excel.xlsx"

with open(filename, "rb") as attachment:
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

encoders.encode_base64(part)
print("Email Sent")
```
This snippet demonstrates sending emails with attachments using the smtplib and email libraries. It allows you to compose and send emails programmatically.

####Converting PDF to Images
```python
import fitz

filename = "test.pdf"
pdf = fitz.open(filename)

for page in pdf:
    img = page.get_pixmap(alpha=False)
    img.writePNG('page-%i.png' % page.number)
```
With the help of the fitz library, this snippet converts each page of a PDF file into individual image files.

####Transforming Text into Art
```python
from art import text2art, tprint, art

print(text2art("default"))
tprint("Python", font="block", chr_ignore=True)
print(art("coffee"))
```
The art library empowers this snippet to convert plain text into artistic representations such as ASCII or stylized text.

####Retrieving File Size
```python
import os

file_path = "excel.xlsx"
size = os.stat(file_path)
filesize = size.st_size
print(filesize, "bytes") # 9520 bytes
```
By utilizing the os library, this snippet retrieves the size of a file in bytes, providing valuable information for file management and analysis.

####Obtaining Wi-Fi Passwords
```python
import subprocess

network = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
profiles = [i.split(":")[1][1:-1] for i in network if "All User Profile" in i]

for profile in profiles:
    results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', profile, 'key=clear']).decode('utf-8').split('\n')
    results = [net.split(":")[1][1:-1] for net in results if "Key Content" in net]
    print("{:<30}|  {:<}".format(profile, results[0]))
```
This snippet uses the subprocess library to retrieve saved Wi-Fi passwords on your computer, simplifying network management.

####URL Shortening
```python
from pyshorteners import Shortener

link = "https://medium.com/"
s = Shortener()
shortened_url = s.tinyurl.short(link)
```
####Getting IP Address
```python
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip_addr = s.getsockname()[0]
s.close()
print(ip_addr)
```
Using the pyshorteners library, this snippet shortens long URLs into more compact and shareable versions.

####Website Screenshotting
```python
from selenium import webdriver
import time

driver = webdriver.Chrome("chromedriver.exe")
driver.maximize_window()
driver.get("https://medium.com/")
time.sleep(3)
driver.save_screenshot("shot.jpg")
```
With the selenium library, this snippet captures screenshots of websites, enabling automated website testing, monitoring, or documentation.

####Unzipping Files
```python
import zipfile

unzip = zipfile.ZipFile("filename.zip")
unzip.extractall()
```
This snippet uses the zipfile library to extract files and folders from a compressed zip file.

####Downloading YouTube Videos
```python
import pytube

link = "Youtube video url here"
youtube = pytube.YouTube(link)
video = youtube.streams.get_highest_resolution()
video.download()
# or
video.download('save location')
```
Utilizing the pytube library, this snippet lets you download YouTube videos in the highest resolution or to a specified location.


####Extracting Exif Data from Photos

```python
import PIL.Image
import PIL.ExifTags

img = PIL.Image.open("img.png")

exif = {
    PIL.ExifTags.TAGS[key]: value
    for key, value in img._getexif().items()
    if key in PIL.ExifTags.TAGS
}
print(exif)
```
With the help of the PIL (Python Imaging Library), this snippet extracts Exif metadata from images, providing information about camera settings and more.

####How To Run a Python File

To run a Python file that contains the provided code snippet on the terminal, follow these step-by-step instructions:

Open a text editor or an Integrated Development Environment (IDE) and create a new file.

Copy and paste the code snippet into the newly created file.
Save the file with a .py extension, such as exif_extractor.py, in a location of your choice.

Open a terminal or command prompt on your computer.

Navigate to the directory where you saved the Python file using the cd command. For example, if the file is saved on the desktop, you can use the following command:

`cd Desktop`

Once you are in the correct directory, you can run the Python file by executing the following command in the terminal:

`python exif_extractor.py`

This assumes that you have Python installed on your system and the python command is configured in your system's PATH variable.

Press Enter to execute the command.

The code will be executed, and if there are no errors, the output containing the extracted Exif data from the provided image file (img.png ) will be displayed in the terminal.

Before running the code, ensure you have installed the necessary dependencies, such as the PIL library.

You can install the required libraries by using the pip package manager. For example, to install the PIL library, you can execute the following command in the terminal:

####Conclusion

You can easily automate tasks by incorporating these 13 powerful Python snippets into your projects.

From fetching search results, measuring network speed, sending emails and converting file formats, these code snippets will significantly enhance your productivity.

Experiment with these snippets and explore the possibilities of automation using Python. Start automating the cool stuff today and enjoy the benefits of streamlined workflows and increased efficiency. Happy coding!

If you discover this, publish thrilling, discover extra thrilling posts like this on <a class="attrlink" href="http://blog.learnhub.africa/" target="_blank" rel="noopener noreferrer nofollow">Learnhub Blog</a>; we write a lot of tech-related topics from <a class="attrlink" href="https://blog.learnhub.africa/category/cloud-computing/" target="_blank" rel="noopener noreferrer nofollow">Cloud computing</a> to <a class="attrlink" href="https://blog.learnhub.africa/category/frontend/" target="_blank" rel="noopener noreferrer nofollow">Frontend Dev</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/security/" target="_blank" rel="noopener noreferrer nofollow">Cybersecurity</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/data-science/" target="_blank" rel="noopener noreferrer nofollow">AI</a> and <a class="attrlink" href="https://blog.learnhub.africa/category/blockchain/" target="_blank" rel="noopener noreferrer nofollow">Blockchain</a>. Take a look at <a class="attrlink" href="https://blog.learnhub.africa/2023/02/16/how-to-build-offline-web-applications/" target="_blank" rel="noopener noreferrer nofollow">How to Build Offline Web Applications. </a>
<h3>Resources</h3>
<ul>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/05/26/20-essential-python-extensions-for-visual-studio-code/" target="_blank" rel="noopener noreferrer nofollow">20 Essential Python Extensions for Visual Studio Code</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/04/27/using-python-for-web-scraping-and-data-extraction/" target="_blank" rel="noopener noreferrer nofollow">Using Python for Web Scraping and Data Extraction</a></li>
 	<li><a class="attrlink" href="https://www.python.org/about/gettingstarted/" target="_blank" rel="noopener noreferrer nofollow">Getting Started with Python</a></li>
</ul>