python win32 COM关闭Excel工作簿
我在 COM 中打开几个不同的工作簿(excel xlsx 格式),然后对它们进行处理。随着程序的进展,我希望关闭一本特定的工作簿,但保持其余工作簿打开。
如何关闭一本工作簿? (而不是整个 Excel 应用程序)
xl = Dispatch("Excel.Application")
xl.Visible = False
try:
output = xl.Workbooks.Open(workbookName)
output2 = xl.Workbooks.Open(workbook2Name)
except com_error:
print "you screwed up blahblahblah"
exit()
#work on some stuff
#close output but keep output2 open
I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open.
How do I close ONE workbook? (instead of the entire excel application)
xl = Dispatch("Excel.Application")
xl.Visible = False
try:
output = xl.Workbooks.Open(workbookName)
output2 = xl.Workbooks.Open(workbook2Name)
except com_error:
print "you screwed up blahblahblah"
exit()
#work on some stuff
#close output but keep output2 open
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Workbook COM 对象有一个 Close() 方法。基本上,它应该是这样的:
上面只是一个框架,下面是一些在我的机器上针对 Office 2010 运行的代码:
当然,这会创建一个新的 xlsx 文件。但随后我能够在同一会话中成功打开并修改文件,如下所示:
不知道这些是否有帮助......
The the Workbook COM object has a Close() method. Basically, it should be something like:
The above was just a skeleton here's some code that works on my machine against Office 2010:
Of course, that creates a new xlsx file. But then I'm able to successfully open and modify the file in the same session as follows:
Don't know if any of that helps...
您还可以尝试使用以下代码:
You can also try to use the following code:
此函数关闭所有打开的 Excel 文件
This function closes any opened excel file