从Python程序获取WinWord版本

时间:2017-11-14 06:38:32

标签: 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技术。

2 个答案:

答案 0 :(得分:1)

使用体系结构检测(32位或64位)搜索shorcuts

我认为附加信息很有用,但你可以把它减少到只有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())

答案 1 :(得分:0)

另一种方法是使用 OLE / ActiveX / COM 技术。这是一种提供“注册表方法”的某种高级版本。 假设您使用的是Windows机器,因为在大多数情况下这不适用于Linux:

#!/usr/bin/env python3
from win32com.client.dynamic import Dispatch

word = Dispatch('Word.Application')
print (word)
word_version = word.version
print (word_version)
  1. 创建virtualenv:$ python -m venv ve
  2. 激活virtualenv:$ ve\Scripts\activate.bat
  3. 安装pypiwin32:(ve)$ pip install pypiwin32
  4. 执行:(ve)$ python detect_msword_version.py
  5. 在我的Windows机器上输出:

    Microsoft Word

    16.0