从 Access VBA 运行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23914644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:17:47  来源:igfitidea点击:

Running a Python script from Access VBA

pythonvbams-access

提问by Peter Chappy

I'm having trouble finding many resources, but I'm trying to get vba to run a python script

我找不到很多资源,但我正在尝试让 vba 运行 python 脚本

Const pyScript = "C:\Test\Weekend_Exceptions\Weekend_Exception.py"
Dim dblRetVal As Double
dblRetVal = Shell("C:\Python34\python.exe " & pyScript)

I know my python script works and should output a file, but its not. Additionally the vba is not tirggering a debug flag so I'm not sure where I am wrong. Any advice would be appreciated.

我知道我的 python 脚本有效并且应该输出一个文件,但它不是。此外,vba 没有触发调试标志,所以我不确定我错在哪里。任何意见,将不胜感激。

回答by Victor Panisa

You don't give too much details so i'll make some assumptions

你没有提供太多细节,所以我会做一些假设

Probably your python script read some local file this will cause your script to raise a FileNotFoundError and exit

可能您的 python 脚本读取了一些本地文件,这将导致您的脚本引发 FileNotFoundError 并退出

To make the test copy the entire arg string to Shell, in your case "C:\Python34\python.exe C:\Test\Weekend_Exceptions\Weekend_Exception.py", open a cmd with Win+r , paste and run, not being in the right directory should raise the same error.

要使测试将整个 arg 字符串复制到 Shell,在您的情况下为“C:\Python34\python.exe C:\Test\Weekend_Exceptions\Weekend_Exception.py”,请使用 Win+r 打开一个 cmd,粘贴并运行,而不是被在正确的目录中应该引发相同的错误。

If this is the problem, make a makestuff.bat file with the code

如果这是问题,请使用代码制作 makestuff.bat 文件

@echo off
@cd C:\Test\Weekend_Exceptions\
@C:\Python34\python.exe Weekend_Exception.py
@echo on

Then call the bat from Shell("C:\Place\of\your\bat\makestuff.bat")

然后从 Shell 调用 bat("C:\Place\of\your\bat\makestuff.bat")

Return with more details to we work on a solution

返回更多详细信息,我们正在制定解决方案

回答by Cairo Mendes

Here is how I did:

这是我的做法:

  1. I created a File1.bat to open the File2.py. The code in File1.bat is:
  1. 我创建了一个 File1.bat 来打开 File2.py。File1.bat 中的代码是:
@echo off
@h:
@cd H:\Path\Cronus\Rangers
@C:\Python3\python.exe File2.py
@echo on
@echo off
@h:
@cd H:\Path\Cronus\Rangers
@C:\Python3\python.exe File2.py
@echo on

Note that File2.py is inside H:\Path\Cronus\Rangers folder. That's why we need to open it before.

请注意 File2.py 位于 H:\Path\Cronus\Rangers 文件夹中。这就是为什么我们需要先打开它。

  1. I created a function in VBA to open a .bat file:
  1. 我在 VBA 中创建了一个函数来打开一个 .bat 文件:
Option Compare Database

Function MacroPythonSARH()
On Error GoTo MacroPythonSARH_Err

Call Shell("H:\Path\Cronus\Rangers\File1.bat", 1)

MacroPythonSARH_Exit:
    Exit Function

MacroPythonSARH_Err:
    MsgBox Error$
    Resume MacroPythonSARH_Exit

End Function
Option Compare Database

Function MacroPythonSARH()
On Error GoTo MacroPythonSARH_Err

Call Shell("H:\Path\Cronus\Rangers\File1.bat", 1)

MacroPythonSARH_Exit:
    Exit Function

MacroPythonSARH_Err:
    MsgBox Error$
    Resume MacroPythonSARH_Exit

End Function