简体   繁体   English

Python使用Win32com关闭工作簿

[英]Python to close a workbook using win32com

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. 我在Windows 10计算机上使用python 2.7.11,在其中使用Update_table.xls工作表中包含的VBA更新文件。 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. 我正在使用以下代码,但是我发现一旦py脚本运行,Excel工作表有时就在任务管理器中保持打开状态。

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

As I am running multiple scripts at once is there a way to ensure the workbook is closed? 当我一次运行多个脚本时,是否有办法确保工作簿已关闭? & not close the whole excel application. &不关闭整个excel应用程序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM