[英]Save excel workbook in Python
I am new in Python and I need to open all Excels in a folder (one by one), wait some time for SAS addin to load new data, then save it and close it.我是 Python 新手,我需要打开一个文件夹中的所有 Excel(一个一个),等待一段时间让 SAS 插件加载新数据,然后保存并关闭它。 I tried to use some libraries, but I cant obtain the expected result.
我尝试使用一些库,但我无法获得预期的结果。
Using subprocess:使用子流程:
import os
import subprocess
path = "C:\\Users\\...\\Desktop\\TEST_EXCEL"
for file in os.listdir(path):
filename = os.path.join(path, file)
if filename.endswith(".xlsx"):
proc = subprocess.Popen([filename], shell=True)
time.sleep(60)
subprocess.call(['taskkill', '/F', '/T', '/PID', str(proc.pid)])
I think the module you will need for this is win32com, with the following code: 我认为您需要的模块是win32com,其代码如下:
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open(filename)
time.sleep(60)
wb.Close(True)
In this situation, wb.Close(True) saves and closes the file. 在这种情况下,wb.Close(True)保存并关闭文件。 Just add that to your loop over listdir and it should work.
只需将其添加到listdir的循环中即可,它应该可以工作。 I hope it helps!
希望对您有所帮助!
Jim, 3 years later, your post is still very valueable.
<\/blockquote>"吉姆,3 年后,你的帖子仍然很有价值。 Apparently, I don't have enough reputation points to give you a mark up.
显然,我没有足够的声望点来给你加分。 :) Thank you.
:) 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.