You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python检测Excel是否安装并获取版本号(Python3.9.7环境)

判断Excel是否安装并获取版本号(Python 3.9.7)

Windows 平台实现

方法1:通过 win32com 调用 Excel COM 对象

直接尝试初始化Excel实例,成功则提取版本号,失败则判定未安装。需先安装依赖包:

pip install pywin32

代码示例:

import win32com.client
from win32com.client import Dispatch

def check_excel_windows():
    try:
        excel = Dispatch("Excel.Application")
        version = excel.Version
        excel.Quit()
        # 版本对应参考:16.0=Office 2016/2019/365,15.0=2013,14.0=2010
        return f"Excel已安装,版本号:{version}"
    except Exception:
        return "未检测到Excel安装"

print(check_excel_windows())

方法2:查询Windows注册表

无需额外依赖,直接读取注册表键值判断:

import winreg

def check_excel_via_registry():
    try:
        with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Excel.Application\CurVer") as key:
            cur_ver = winreg.QueryValue(key, None)
            version = cur_ver.split(".")[-1]
            return f"Excel已安装,版本号:{version}.0"
    except FileNotFoundError:
        return "未检测到Excel安装"

print(check_excel_via_registry())

macOS 平台实现

通过检查应用目录并读取Info.plist文件获取版本:

import plistlib
import os

def check_excel_mac():
    excel_path = "/Applications/Microsoft Excel.app"
    if not os.path.exists(excel_path):
        return "未检测到Excel安装"
    
    info_plist = os.path.join(excel_path, "Contents/Info.plist")
    with open(info_plist, "rb") as f:
        plist_data = plistlib.load(f)
        version = plist_data.get("CFBundleShortVersionString")
        return f"Excel已安装,版本号:{version}"

print(check_excel_mac())

跨平台兼容方案

根据操作系统自动选择检测逻辑:

import sys

def check_excel():
    if sys.platform.startswith("win"):
        return check_excel_windows()  # 可替换为注册表查询方法
    elif sys.platform.startswith("darwin"):
        return check_excel_mac()
    else:
        return "暂不支持当前操作系统检测Excel"

print(check_excel())

内容的提问来源于stack exchange,提问作者Kent Choo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 19:15:52