更改RunPython导入的工作目录

问题描述 投票:0回答:1

我正在尝试从Excel VBA宏运行Python脚本。最初我没有问题,因为.py文件与excel文件位于同一目录中。现在,我们要将.py文件存储在其他目录中,并且宏会产生错误。

原始宏:

RunPython ("import pythonefile; pythonfile.get_data()")

我想执行以下操作,但不确定如何更改宏从中导入.py文件的目录。

ChDir "C:\"
RunPython ("import pythonfile; pythonfile.get_data()"
python excel vba xlwings chdir
1个回答
0
投票

有同样的问题。原来,您需要更改xlwings的默认路径。在VB编辑器中,转到xlwings.xlam(如果需要输入密码,则为“ xlwings”),打开modules文件夹,然后选择模块“ main”。在Sub ExecuteWindows中添加Wsh.currentdirectory =“ C:\”,如下图所示。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.