python Win32 保存 [英] python Win32 Save

查看:54
本文介绍了python Win32 保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 打开一个 excel 文件并进行一些更改,然后我需要保存它.除了保存部分,我可以完成所有工作.如何让python保存文件.如果我手动保存它,我会得到弹出框,我可以点击保存,但我不想手动点击保存.我希望python只保存它并关闭excel.我需要帮助,这就是我所拥有的.但它不会工作.我可以让它保存但不能关闭????

I am using python to open an excel file and make some changes and then I need to save it. I can get everything done except the save part. How do I get python to save the file. If I save it manual I get the pop up box and I can click save, but I don't want to have to manually click save. I want python to just save it and close excel. I need help, this is what I have. but it won't work. I can get it to save but not close????

 import win32com.client
 import os


 xl = win32com.client.DispatchEx("Excel.Application")
 xl.workbooks.open("C:\file.xlsm")
 xl.run("file.xlsm!macro")
 xl.Visible = True
 xl.save = true
 xl.close

推荐答案

您需要获取到工作簿对象的钩子并将保存为参数应用于 close 方法:

You need to get a hook to the workbook object and apply the save as an argument to the close method:

xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.workbooks.open("C:\file.xlsm")
xl.run("file.xlsm!macro")
xl.Visible = True
wb.Close(savechanges=1)
xl.Quit()

这篇关于python Win32 保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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