首页 > 解决方案 > Pywin32 和 Office365

问题描述

我最近从 Office 2013 降级到 Office365,现在我的 Python 命令行脚本不再在脚本末尾退出,也不会返回任何类型的错误。我的脚本调用了我编写的帮助文件:

import pywintypes
import win32com.client as win32

def autofit(path):
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    try:
        wb = excel.Workbooks.Open(str(path))
        if wb is None:
            print("Excel could not open %s" % path)
            return

        for sh in wb.Sheets:
            wb.Sheets(sh.Name).Columns.AutoFit()
        wb.Save()
        wb.Close(True)
    except pywintypes.com_error:
        print("Autofit could not open %s with Excel" % path)
    excel = None

这在过去是可靠的。现在,当我在命令行上运行脚本时,它只是挂起。这个 autofit 命令是我在脚本中做的最后一件事。我已经升级到 pywin32 版本 301,问题依旧。我必须按 CTRL+BREAK 来阻止脚本运行并且文件不再获得自动调整。

这是在 Windows 10 上。

任何想法为什么这在 Excel 2013 中有效但在 365 中无效?

标签: pythonpywin32

解决方案


推荐阅读