给Python .exe输入数据,来自VBA

huangapple go评论87阅读模式
英文:

Giving Input to a Python .exe from VBA

问题

    1. 在Excel中打开一个界面,允许用户选择一些数据(文件路径等)。
    1. 以某种方式将输入数据传递给可执行文件。

我目前正在使用Tkinter来打开一个用于输入的界面。然而,我对Tkinter的技能不是很好,感觉它使我的程序变慢了。

我认为我可以解决第一部分。有没有办法做到这一点?我唯一的想法是不将我的Python脚本转换为可执行文件,而是从VBA调用脚本,这样传递数据会更容易(我认为)。

任何帮助/建议都将不胜感激,谢谢!

我的VBA代码看起来像这样:(这可能不会有太多帮助..)

Sub RunPython()

Dim objShell As Object
Dim PythonExe, PythonScript As String
    
    Set objShell = VBA.CreateObject("Wscript.Shell")

    PythonExe = """C:\your_path\Python\Python38\python.exe"""
    PythonScript = "C:\your_path\from_vba.py"
    
    objShell.Run PythonExe & PythonScript
    
End Sub

我曾经有个不太好的主意,那就是将输入数据写入一个文本文件,然后让Python可执行文件从那里读取。这将避免使用Tkinter,至少可以这么说。我知道将Tkinter学得滚瓜烂熟是最好的选择,但我在需要正确使用Tkinter的面向对象编程方面有困难。

英文:

I have a project where I want I am running a Python executable from VBA. What I would like to do is the following:

    1. In Excel open an Interface that lets the user select some data (filepaths etc.)
    1. Somehow feed the input data to the Exe.

Right I am using Tkinter to open an Interface for the inputs. However my skills with tkinter are not that great and I feel it makes my programm slow.

I think I can figure out part 1.
Is there a way of doing this?
My only idea would be to not convert my Python scripts into an executable and calling the script from VBA which would make passing data to it easier (i think)

Any help/tips are appreciated, Thanks!

My VBA code looks something like this: (this probably wont help much..)

Sub RunPython()

Dim objShell As Object
Dim PythonExe, PythonScript As String
    
    Set objShell = VBA.CreateObject("Wscript.Shell")

    PythonExe = """C:\your_path\Python\Python38\python.exe"""
    PythonScript = "C:\your_path\from_vba.py"
    
    objShell.Run PythonExe & PythonScript
    
End Sub

One sloppy idea I had is to write the Input data to a txt file and let the Python exe read from there. This would avoid the usage of Tkinter atleast. I know learning tkinter by heart would be the best option but I am struggling with the OO programming which is appearently needed to use Tkinter properly.

答案1

得分: 0

最佳选择取决于您需要解析多少数据以及是否希望在某些情况下手动调用Python。

命令行方法

如果您想使用命令行方法,那么您的大部分内容都是正确的,除了您没有在python.exe和python命令之间包含空格。这一行:

objShell.Run PythonExe & PythonScript

翻译为:“C:\your_path\Python\Python38\python.exe”C:\your_path\from_vba.py

一个稍微更清晰和易于维护的方法可能是将其放入一个字符串中,如:

PythonScript = """c:\python_path\python.exe"" ""c:\python_script\from_vba.py"" --input1 ""input1 value"" --input2 ""input 2 value"""

或者您可以将每个组件放入数组中,然后使用" "作为分隔符将它们连接成一个单一的字符串。

使用这种方法,您可以使用Python模块argparse来读取这些参数并相应地处理它们。

INI文件方法

如果您需要解析大量输入,那么命令行可能会很快变得混乱,而我会建议您使用.TXT文件,除了使用.INI文件,它比.TXT文件更有结构。

这里有一个用于读取/写入.INI文件的示例VB代码链接

Python模块configParser可以用于将.INI文件读取为类似字典的对象。

这种方法的好处包括:

  • 可以添加/删除字段,而不容易破坏您的代码

  • 可以提供更复杂的数据

  • 可以解析双向数据。例如,您可以让Python脚本修改.INI文件,以记录上次执行的结果(成功、失败等)。然后,VBA表单可以读取.INI文件并将其显示给用户,从而提供更流畅的用户体验。

英文:

The best choice comes down to how much data you need to parse and if you want to manually call python in some instances.

Command Line Approach

If you wanted to do the command line approach, then what you have is mostly correct, except you didn't include a space between python.exe and the python command. The line:

objShell.Run PythonExe & PythonScript

translates to: "C:\your_path\Python\Python38\python.exe"C:\your_path\from_vba.py

A slightly cleaner and easier to maintain approach may be to put it in 1 x string instead such as:

PythonScript = """c:\python_path\python.exe"" ""c:\python_script\from_vba.py"" --input1 ""input1 value"" --input2 ""input 2 value"""

Or you could put each component into an array and join it back to a single string with " " as the separator

With this approach, you can then use the python module argparse to read these arguments and process accordingly

INI File Approach

If you have a lot of inputs to parse, then command line can become messy very quickly and instead I'd go with you suggestion of a .TXT file, excpet use a .INI file, which is more structured than a .TXT file.

There's example VB code for reading/writing .INI files here.

Python module configParser can be used to read the .INI file in a dictionary like object

The benefits of this approach are:

  • Fields can be added / removed with less risk of breaking your code
  • More complex data can be provided
  • Bi-directional data can be parsed. For example, you could have the Python script modify the .INI file with the last execution result (success, failure, etc). The VBA form could then read the .INI file and display it to the user, which provides a more seamless user experience.

huangapple
  • 本文由 发表于 2023年8月4日 07:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定