Python to Battery Notificator with Full Source Code

This python script Battery Notificator gives you a notification about the battery percentage of the device.

Pre-requisites:

  • You will need to install python on your machine.
  • You can download python from python.org and install it.
  • And a few other python packages that you need to install are as:
1. psutil
    > pip install psutil

2. pynotifier
    > pip install py-notifier

3. win10toast
    > pip install win10toast

How to run the Script:

python battery.py
Code language: CSS (css)

Requirements:

  • psutil==5.7.2
  • py-notifier==0.1
  • win10toast==0.9

Souce Code:

# pip install psutil
import psutil

battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent

if percent >= 30:
 
    # pip install py-notifier
    # pip install win10toast
    from pynotifier import Notification

    Notification(
        title="Battery Low",
        description=str(percent) + "% Battery remain!!",
        duration=5,  # Duration in seconds
        urgency=Notification.URGENCY_CRITICAL,
    ).send()

Leave a Comment