win32com获取可用应用程序列表


有一个很好的构造,允许控制win32com对象,比如

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

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

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

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

转载请注明出处:http://www.eluwawa.com/article/20230526/2246754.html