Getting Battery Capacity in Windows with Python

The most reliable way to retrieve this information is by using GetSystemPowerStatus instead of WMI. psutil exposes this information under Linux, Windows and FreeBSD:

>>> import psutil
>>>
>>> def secs2hours(secs):
...     mm, ss = divmod(secs, 60)
...     hh, mm = divmod(mm, 60)
...     return "%d:%02d:%02d" % (hh, mm, ss)
...
>>> battery = psutil.sensors_battery()
>>> battery
sbattery(percent=93, secsleft=16628, power_plugged=False)
>>> print("charge = %s%%, time left = %s" % (battery.percent, secs2hours(battery.secsleft)))
charge = 93%, time left = 4:37:08

The relevant commit is here.


Tim Golden's excellent wmi module will, I believe, give you everything you want. You'll just have to do several queries to get everything:

import wmi

c = wmi.WMI()
t = wmi.WMI(moniker = "//./root/wmi")

batts1 = c.CIM_Battery(Caption = 'Portable Battery')
for i, b in enumerate(batts1):
    print 'Battery %d Design Capacity: %d mWh' % (i, b.DesignCapacity or 0)


batts = t.ExecQuery('Select * from BatteryFullChargedCapacity')
for i, b in enumerate(batts):
    print ('Battery %d Fully Charged Capacity: %d mWh' % 
          (i, b.FullChargedCapacity))

batts = t.ExecQuery('Select * from BatteryStatus where Voltage > 0')
for i, b in enumerate(batts):
    print '\nBattery %d ***************' % i
    print 'Tag:               ' + str(b.Tag)
    print 'Name:              ' + b.InstanceName

    print 'PowerOnline:       ' + str(b.PowerOnline)
    print 'Discharging:       ' + str(b.Discharging)
    print 'Charging:          ' + str(b.Charging)
    print 'Voltage:           ' + str(b.Voltage)
    print 'DischargeRate:     ' + str(b.DischargeRate)
    print 'ChargeRate:        ' + str(b.ChargeRate)
    print 'RemainingCapacity: ' + str(b.RemainingCapacity)
    print 'Active:            ' + str(b.Active)
    print 'Critical:          ' + str(b.Critical)

This is certainly not cross-platform, and it requires a 3rd party resource, but it does work very well.