如何从 win32com.client.dispatch("Shell.Application") 获取属性 [英] how to get attributes from win32com.client.dispatch("Shell.Application")

查看:63
本文介绍了如何从 win32com.client.dispatch("Shell.Application") 获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 python 以编程方式控制我的设备管理器(即禁用和重新启用设备).但是,我无法弄清楚win32com.client.Dispatch(Shell.Application")"的命名空间中的属性是什么.我所知道的就是获取名称并打印出来.我对代码进行了调试,但找不到任何有用的东西.

I am trying to control my device manager programmatic through python (ie disable and re-enabling devices). However I am having trouble figuring out what are the attributes in the namespace of the "win32com.client.Dispatch("Shell.Application")". All i know how to do is get the name and print it. I did a debugging run through the code but i couldn't find anything useful.

这是我目前所拥有的

    import win32com.client
    shell = win32com.client.Dispatch("Shell.Application")
    control_panel = shell.Namespace(3)
    for item in control_panel.Items():
        if item.Name == "Device Manager":
            print item
            break

这也不是很有用:

 control_panel.GetNamespace("MAPI")
 Traceback (most recent call last):
   File "<interactive input>", line 1, in <module>
   File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
 AttributeError: <unknown>.GetNamespace

推荐答案

检查 COM 对象中属性的一种方法是使用 win32com\client 上提供的 combrowse.pycode> 在你的 python 站点包文件夹中

One way to check the attributes in a COM object is using the combrowse.py available on win32com\client in your python site-packages folder

只需运行脚本(双击或从命令行/python),就会出现一个窗口,其中包含所有可用的 com 对象.在 Registered Type Libraries 下,您应该在 Microsoft Shell Controls And Automation
中的相应库下找到 Shell您可以在 python 中使用以下命令进行检查:

Just run the script (double clicking or from command line/python) and a window should appear with all the available com objects. Under Registered Type Libraries you should find the Shell under the correspondent Library in Microsoft Shell Controls And Automation
You can check this with the following command in python:

from win32com.client import gencache
shell = gencache.EnsureDispatch('Shell.Application')
print shell

此外,使用 gencache 方法,您可以使用 Tab 来检查一些可用的方法,但要获得完整列表,请查看 combrowse.py.唯一的问题是 combrowse.py 中列出的一些方法在 python 中并不真正可用.

Also, using the gencache method, you can use Tab to check some of the available methods, but for a comprehensive list check the combrowse.py. The only issue is that some of the methods listed at the combrowse.py are not really available within python.

这篇关于如何从 win32com.client.dispatch("Shell.Application") 获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆