【发布时间】:2020-04-17 12:08:37
【问题描述】:
您好,我正在使用此处提供的答案 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
从哪里知道我该如何解决这个问题??
谢谢,
【问题讨论】: