Python 使用 win32com 关闭工作簿 [英] Python to close a workbook using win32com

查看:71
本文介绍了Python 使用 win32com 关闭工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 10 机器上使用 python 2.7.11,我正在使用 Update_table.xls 工作表中包含的 VBA 更新文件.我正在使用以下代码,但我发现一旦 py 脚本运行,Excel 工作表有时会在任务管理器中保持打开状态.

I'm using python 2.7.11 on a windows 10 machine where I'm updating a file using VBA contained in the Update_table.xls worksheet. I'm using the below code but I am finding that the Excel worksheet sometimes remains open in the task manager once the py script has ran.

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\Projects\Update_table.xlsb",ReadOnly=1)
xl.Application.Run("Update2")
xl = 0

当我同时运行多个脚本时,有没有办法确保工作簿关闭?&不关闭整个 Excel 应用程序.

As I am running multiple scripts at once is there a way to ensure the workbook is closed? & not close the whole excel application.

推荐答案

不确定一次运行多个脚本是什么意思,但要关闭工作簿:

Not sure what you mean by running multiple scripts at once, but to close the workbook:

wb = xl.Workbooks.Open(Filename="C:\Projects\Update_table.xlsb",ReadOnly=1)
...
wb.Close()
wb = None

这篇关于Python 使用 win32com 关闭工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆