python - 从 Python 程序获取 WinWord 版本

标签 python windows

在一个用于测试用户环境的简单 Python 程序中,我还应该获取已安装 Word 的版本(例如:WinWord 16、64 位)。 知道怎么做吗?我敢肯定,应该存在不同的 GUID,但我在哪里可以找到它们:-)

**** 编辑

我检查了这两个建议,但这还不够。如果 Word 是 32 位或 64 位,我必须获得信息。到目前为止,我已经检查了“winword.exe”的位置。例如: 1. 64 位 Office 15 的位置 c:\program files\microsoft office\office15\winword.exe 2. 32 位 Office 15 的位置 c:\program files (x86)\microsoft office\office15\winword .exe

我确定 M$ 有最新版本 Word 的 GUID 列表(在注册表中使用)。但我找不到列表 :-) 使用建议的解决方案,我无法检查 32 或 64 技术。

最佳答案

使用架构检测(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 下的所有注册表项并找到最近的一个。不幸的是,Microsoft 认为更改几乎每个版本的安装目录是个好主意。因此,如果您想编制一份包含所有安装位置的完整列表,那是可行的(除非他们将其安装在自定义位置)。

注意:这是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: #relies on error handling WindowsError as e as well as type conversion when we run out of numbers
            break
    return versionNum


print(getMicrosoftWordVersion())

关于python - 从 Python 程序获取 WinWord 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279167/

相关文章:

c++ - 从 C++ 中的非常大的文本文件资源中读取

python - Python 中的 "ValueError: list.index(x): x not in list",但它存在

python - 使用 django ORM 进行高级排序

windows - 了解 AuthServerWhitelist 和 AuthNegotiateDelegateWhitelist chrome 策略寄存器?

windows - conemu 中的 cygwin 捕获键绑定(bind)

c++ - cmake force config=库的发布

python - 从其他数据框中选择具有特定条件的行

python - 如何在 Tensorflow 中结合 feature_columns、model_to_estimator 和数据集 API

python - Tastypie:queryset = Model.objects.all()

windows - 无法在终端窗口中输入