win32com获取可用应用程序列表

时间:2018-08-10 16:55:03

标签: python win32com

有一个很好的结构,可以控制win32com对象,例如

win32com.client.Dispatch('Word.Application')
win32com.client.Dispatch('Excel.Application')

...等 有没有办法获取可用应用程序的完整列表(* .Application)?

UPD。通过扫描注册表项HKEY_CLASSES_ROOT中的CLSID条目来获取列表的可能解决方案。

from collections import OrderedDict
def myScan():

    result = OrderedDict(); i = 0
    while True:
        try:
            a1 = winreg.OpenKey(aReg, winreg.EnumKey(winreg.HKEY_CLASSES_ROOT, i))
            if i%1000==0:
                print(i,'processing')
            j = 0
            while True:
                try:
                    #print(j)
                    a2 = winreg.EnumKey(a1,j)
                    if a2=='CLSID':
                        kkkey = winreg.EnumKey(winreg.HKEY_CLASSES_ROOT,i)
                        vvvalue = winreg.EnumValue(winreg.OpenKey(a1,a2),0)
                        result[kkkey] = vvvalue
                        break
                    j+=1
                except OSError:
                    break
            i+=1
        except OSError:
            break
    return result

result = myScan() #candidates
print(len(res)) # 3363
keys_ = list(result.keys())
#retrieving part of result
for i in keys_[:10]:
    print(i,'=>',result[i])
"""
AccClientDocMgr.AccClientDocMgr => ('', '{FC48CC30-4F3E-4fa1-803B-AD0E196A83B1}', 1)
AccClientDocMgr.AccClientDocMgr.1 => ('', '{FC48CC30-4F3E-4fa1-803B-AD0E196A83B1}', 1)
AccDictionary.AccDictionary => ('', '{6572EE16-5FE5-4331-BB6D-76A49C56E423}', 1)
AccDictionary.AccDictionary.1 => ('', '{6572EE16-5FE5-4331-BB6D-76A49C56E423}', 1)
Access.ACCDAExtension => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
Access.ACCDCFile => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
Access.ACCDCFile.14 => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
Access.ACCDEFile => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
Access.ACCDEFile.14 => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
Access.ACCDRFile.14 => ('', '{73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9}', 1)
"""

通过下面的代码过滤列表(操作较长且可能不安全),可以获得列表:

import pywintypes
for i in range(len(keys_)):
    try:
        if i%100==0: print(i,'processing')
        app = win32com.client.Dispatch(keys_[i]) #possibly unsafe; for example, word.Quit() is necessary to do
        finalList.append(keys_[i])
    except pywintypes.com_error:
        pass

1 个答案:

答案 0 :(得分:4)

使用专用的COM浏览器可能会有所帮助。例如,您可以使用与win32com捆绑在一起的COM浏览器来研究可用的类型库及其接口:

from win32com.client import combrowse
combrowse.main()

enter image description here

有关如何从类型库CLSID转到调度时需要的see this question的ProgID的信息。