您好,我正在使用这里提供的答案Running an Excel macro via Python?
代码:
filename = "NewVba.xlsm"
if os.path.exists(filename):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(os.path.abspath(filename), ReadOnly=1)
xl.Application.Run(filename+"!"+ "PK_new_try")
## xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
xl.Application.Quit() # Comment this out if your excel script closes
del xl它运行得很好,唯一的问题是我将文件放在其他文件夹中,并提供了完整的路径,如下所示filename = "C:\Users\kamathp\Downloads\ExcelVBAs\NewVba.xlsm"
我得到一个错误:
File "<input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape从哪里得到的线索我怎么解决这个问题??
谢谢,
发布于 2020-04-17 21:38:41
你的错误说明了一切。您需要避开escape character \
这意味着您的路径需要如下所示:"C:\\Users\\kamathp\\Downloads\\ExcelVBAs\\NewVba.xlsm"
https://stackoverflow.com/questions/61271298
复制相似问题