Advertisement
Python253

batstats.py

May 6th, 2024 (edited)
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: batstats.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script retrieves battery information using the wmi module.
  9. It's designed for Windows, requiring Python 3.x and wmi.
  10. The script presents battery details like status and charge level clearly.
  11. Users can quickly gauge battery health and make informed decisions about power usage.
  12.  
  13. Requirements:
  14.    - Python 3.x
  15.    - wmi module
  16.  
  17. Usage:
  18.    - Before running the script, ensure you have Python installed on your system.
  19.    - Make sure the `wmi` module is installed. You can install it using pip:
  20.            
  21.            'pip install wmi'
  22.        
  23.    - Navigate to the directory containing this script in the terminal.
  24.    - Run the script using the following command:
  25.            
  26.            'python batstats.py'
  27.  
  28. Example Output:
  29.  
  30.        --------------------------------------------------
  31.                :: BATTERY STATS ::
  32.        --------------------------------------------------
  33.  
  34.        Internal Battery: [AP18E8M]    Status: OK
  35.        Battery Status:                Connected to AC (2)
  36.        Charge Remaining:              95%
  37.  
  38.        --------------------------------------------------
  39. """
  40.  
  41. import wmi
  42.  
  43. # Initialize WMI interface
  44. c = wmi.WMI()
  45.  
  46. # Get battery information
  47. battery = c.Win32_Battery()[0]
  48.  
  49. # Dictionary to map battery status codes to their descriptions
  50. battery_status = {
  51.     1: 'Discharging',
  52.     2: 'Connected to AC',
  53.     3: 'Fully charged',
  54.     4: 'Low',
  55.     5: 'Critical',
  56.     6: 'Charging',
  57.     7: 'Charging/High',
  58.     8: 'Charging/Low',
  59.     9: 'Charging/Critical',
  60.     10: 'Undefined',
  61.     11: 'Partially Charged'
  62. }
  63.  
  64. # Get battery status description based on the status code
  65. battery_status_description = battery_status.get(battery.BatteryStatus, 'Unknown')
  66.  
  67. # Print battery information header
  68. print("-" * 50)
  69. print("\t\t:: BATTERY STATS ::")
  70. print("-" * 50)
  71.  
  72. # Print battery information
  73. print(f"\n{battery.Caption}: [{battery.Name}]    Status: {battery.Status}")
  74. print(f"Battery Status:                {battery_status_description} ({battery.BatteryStatus})")
  75. print(f"Charge Remaining:              {battery.EstimatedChargeRemaining}%\n")
  76. print("-" * 50)
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement