Pywin32和Office365

2024-10-01 09:42:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近将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键才能停止脚本的运行,并且文件不再自动调整

这是在Windows10上

你知道为什么这在Excel 2013中有效,但在365中无效吗


Tags: 文件path命令行import脚本nonenotexcel