我在python中运行如下代码来打开excel并运行宏。基本上我的Python脚本是坐在打开excel文件从python中的相对文件路径运行宏
C:\Users\adrlee\Desktop\Python files\Automation
和我的excel VBA文件(Automation.xlsb)正坐在
C:\Users\adrlee\Desktop\Python files\Automation\Powerpoint
我运行此代码
fileDir = os.path.dirname(os.path.realpath('__file__'));
filename = os.path.join(fileDir, '../Powerpoint/Automation.xlsb')
filename = os.path.abspath(os.path.realpath(filename))
print(filename);
if os.path.exists("Powerpoint/Automation.xlsb"):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(filename)
xl.Application.Quit() # Comment this out if your excel script closes
del xl
print("Powerpoint generated");
但我正在逐渐错误
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find C:\\Users\\adrlee\\Desktop\\Python files\\Powerpoint\\Automation.xlsb. Is it possible it was moved, renamed or deleted?", 'xlmain11.chm', 0, -2146827284), None)
我在做什么错
你是否尝试通过手动给出完整路径来运行代码,只是为了检查它的工作? –
它只有当我做了绝对路径的正斜杠时才起作用。但是,这不能作为这个文件可能部署到其他人的计算机上。因此,我在看相对路径 – Adam
hi @ YowE3K请问在开放语句中你的意思是状态完整路径?是不是问题在这里,我不能将Excel文件传入workbooks.open()? – Adam