Excel Python保存到新目录

时间:2017-09-28 15:00:13

标签: python excel python-2.7

我正在尝试将excel保存到新目录中。让我们说如果不同计算机上的某个人试图通过读取excel文件来运行代码。他们将无法在他们的计算机上运行代码,因为该目录位于我的计算机上。我知道每个人都有F:\Users\。如何自动将excel文件保存到其他用户的目录?每次运行代码时,还会要求他们重新保存文件示例:Are you sure you want to replace file?有更好的方法吗?

xlApp = win32com.client.DispatchEx('Excel.Application') # Running Excel
xlsPath = os.path.expanduser('I:\TPGeneral\Ten Year Load Forecasts 2017-2026.xlsm')# Reading xlsm file
wb = xlApp.Workbooks.Open(Filename=xlsPath) # Opening file
xlApp.Run('csvfile2')# Running macro---- csvfile2 is the macro name. It is under the "csv" module in the VBA editor
wb.Save()
xlApp.Quit()

1 个答案:

答案 0 :(得分:0)

如果要将现有文件复制到另一个目录,可以使用:

from shutil import copyfile
copyfile(sourceFoldersPath + name, targetFolderPath + name)

关于权限,请查看warning in the doc。 它从根本上说:根据你的操作系统,一些元数据会丢失。 编辑:警告谈论shutil.copy()shutil.copy2()。另外,shutil.copyfile的文档字符串指出:"复制内容(无元数据)[...]"

相关问题