我正在运行一个代码,其中excel将保存到另一个目录中并且可以正常运行。我的问题是它改为只读文件,如果我想对文件进行更改,我必须用新名称重新保存它。有没有办法让它可以对保存到F盘中的excel文件进行编辑?因为我的其余代码依赖于PATH中的文件。因此,如果我更改excel文件中的数据,我必须更改excel文件的名称,我的其余代码将只读取原始文件中的数据。
PATH='F:\Ten Year Load Forecasts.xlsm'
Master_Copy='I:\Ten Year Load Forecasts.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(PATH)# 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()
#wb.close()
xlApp.Quit()
else:
#copyfile(Master_Copy, PATH)
"Save Master_Copy to Path"
"Repeat Function From above with running macro"
xlApp = win32com.client.DispatchEx('Excel.Application') # Running Excel
xlsPath = os.path.expanduser(Master_Copy)# Reading xlsm file
wb = xlApp.Workbooks.Open(Filename=xlsPath) # Opening file
xlApp.Run('csvfile4')# Running macro---- csvfile3 is the macro name. It is under the "csv" module in the VBA editor
#wb.Save()
xlApp.Quit()
#wb.close()
- 这是csv文件4 VBA代码
Sub csvfile4()
Application.DisplayAlerts = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
"F:\Ten Year Load Forecasts 2017-2026.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ThisWorkbook.Worksheets("PSSE_Export_Data").Copy
ActiveWorkbook.SaveAs Filename:="F:\Load_Forecasts.csv", FileFormat:=xlCSV
End Sub
答案 0 :(得分:0)
保存工作簿时尝试指定“ReadOnlyRecommended:= False”。比如...
ActiveWorkbook.SaveAs Filename:= _
"F:\Ten Year Load Forecasts 2017-2026.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False