路径存在于Excel Python代码中
我正在处理一个目录代码,如果文件存在于指定的路径中,它将在excel上运行一个宏。对于我的else,如果路径不存在,则文件将从I驱动器的路径存在于Excel Python代码中,python,excel,python-2.7,Python,Excel,Python 2.7,我正在处理一个目录代码,如果文件存在于指定的路径中,它将在excel上运行一个宏。对于我的else,如果路径不存在,则文件将从I驱动器的主副本保存到F驱动器,然后运行代码。我该怎么做 import os import os.path PATH='F:\Ten Year Load Forecasts 2017-2026.xlsm' Master_Copy='I:\Ten Year Load Forecasts 2017-2026.xlsm' if os.path.isfile(PATH) an
主副本保存到F
驱动器,然后运行代码。我该怎么做
import os
import os.path
PATH='F:\Ten Year Load Forecasts 2017-2026.xlsm'
Master_Copy='I:\Ten Year Load Forecasts 2017-2026.xlsm'
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
print "File exists and is readable"
xlApp = win32com.client.DispatchEx('Excel.Application') # Running Excel
xlsPath = os.path.expanduser('F:\Ten Year Load Forecats\Ten Year Load Forecasts 2017-2026.xlsm)# Reading xlsm file
wb = xlApp.Workbooks.Open(Filename=xlsPath) # Opening file
xlApp.Run('csvfile3')# Running macro---- csvfile3 is the macro name. It is under the "csv" module in the VBA editor
wb.Save()
xlApp.Quit()
else:
"Save Master_Copy to Path"
"Repeat Function From above with running macro"
所以你问“我怎么复制文件?”?看一看,我尝试使用该方法,但我总是收到“权限被拒绝”消息。请确保您使用的是原始字符串,否则由于转义序列,此代码将在某个时候中断:@Danny,好吧。也许这会很有用:所以你在问“如何复制文件?”?看一看,我尝试使用该方法,但我总是收到“权限被拒绝”消息。请确保您使用的是原始字符串,否则由于转义序列,此代码将在某个时候中断:@Danny,好吧。也许这会有用: