使用Python获取Windows电池容量

时间:2013-05-05 01:00:18

标签: python windows ctypes pywin32 battery

我希望了解当前的电池容量和设计容量​​。

到目前为止,我可以开始工作的是使用Win32_Battery() class,它不会提供我需要的所有信息(至少在我的系统上没有)。我使用了纯python wmi library

另一方面,我发现这有效In Python, how can I detect whether the computer is on battery power?,但不幸的是它没有提供有关容量的任何信息。

Battery Information structureBattery Status structure似乎是完美的。现在我知道我必须使用DeviceIoControl function这样做,我发现这个C++ code可以解释一下。

我更喜欢只使用ctypes的东西而不是pywin32提供的python win32api。 如果你有想法如何在python中这样做,请告诉我!

提前致谢。

6 个答案:

答案 0 :(得分:8)

我相信Tim Golden的优秀wmi模块将为您提供您想要的一切。您只需要执行多个查询即可获得所有内容:

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)

这当然不是跨平台的,它需要第三方资源,但它确实能够很好地工作。

答案 1 :(得分:6)

检索此信息的最可靠方法是使用GetSystemPowerStatus而不是WMI。 psutil在Linux,Windows和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

相关提交是here

答案 2 :(得分:1)

import psutil

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

可变百分比将具有电池百分比。 我已经用python写了一小段代码来通知电池何时充满电

https://github.com/Mitzzzzz/Battery-Full-Indicator

请检查!

答案 3 :(得分:1)

**Checking the battery percentage in python can be done using the psutil module as 
follows **

# Installing the psutil module
from  pip._internal import  main
main(["install", "psutil"])
import psutil

battery = psutil.sensors_battery();

# checking if the charger is plugged
if battery.power_plugged:
    print("Charging: ", battery.percent)
else:
    print("Not Charging", battery.percent ,"%");
    print( "Discharge time ", int(battery.secsleft)," sec_lft")

答案 4 :(得分:0)

使用 Python 了解电池最简单的形式是使用 Psutil 模块...

def build(self):

答案 5 :(得分:0)

试试这个代码

import psutil
import time
battery = psutil.sensors_battery()
while True:
    print("Battery percentage : ", battery.percent)
    print("Power plugged in : ", battery.power_plugged)
    if battery.percent < 35 and battery.power_plugged == False:
        print("Your battry is low")
    time.sleep(5)

但是这段代码会一直运行直到被中断