使用架构检测(32位或64位)搜索快捷方式
我认为额外的信息可能会有用,但您可以将其缩减为只包括32位和64位。
import subprocess, os, struct
def getWordVersion():
directory = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs"
wordFile = ""
wordVersion = ""
def recursiveSearch(directory):
for root, dirs, filenames in os.walk(directory):
for dire in dirs:
result = recursiveSearch(root + "\\" + dire)
if result:
return result
for filename in filenames:
if filename.endswith(".lnk") and filename.startswith("Word "):
wordFile = root + "\\" + filename
wordVersion = filename[:-4]
return wordFile, wordVersion
return False
wordFile, wordVersion = recursiveSearch(directory)
lnkTarget = subprocess.check_output(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
"(New-Object -ComObject WScript.Shell).CreateShortcut('" + wordFile + "').TargetPath"],shell=True)
locationOfLnk = lnkTarget.strip()
IMAGE_FILE_MACHINE_I386=332
IMAGE_FILE_MACHINE_IA64=512
IMAGE_FILE_MACHINE_AMD64=34404
f=open(locationOfLnk, "rb")
f.seek(60)
s=f.read(4)
header_offset=struct.unpack("<L", s)[0]
f.seek(header_offset+4)
s=f.read(2)
machine=struct.unpack("<H", s)[0]
architecture = ""
if machine==IMAGE_FILE_MACHINE_I386:
architecture = "IA-32 (32-bit x86)"
elif machine==IMAGE_FILE_MACHINE_IA64:
architecture = "IA-64 (Itanium)"
elif machine==IMAGE_FILE_MACHINE_AMD64:
architecture = "AMD64 (64-bit x86)"
else:
architecture = "Unknown architecture"
f.close()
return wordVersion + " " + architecture
print(getWordVersion())
注册表方法
一种方法是循环遍历 Office
下的所有注册表键,并找到最新的键。不幸的是,微软几乎在每个版本中都决定更改安装目录。因此,如果您想编译所有安装位置的完整列表,那也可以(除非他们将其安装在自定义位置)。
注意:这是 Python 3 代码,如果您需要 Python 2,则将 winreg
更改为 _winreg
。
import winreg
def getMicrosoftWordVersion():
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Office", 0, winreg.KEY_READ)
versionNum = 0
i = 0
while True:
try:
subkey = winreg.EnumKey(key, i)
i+=1
if versionNum < float(subkey):
versionNum = float(subkey)
except:
break
return versionNum
print(getMicrosoftWordVersion())
+ CategoryInfo : ObjectNotFound: (./SamplePowershell:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException