运行 Python 代码用 VBA 代码调用

Running Python code by calling it with VBA code

我无法通过使用 VBA 宏调用此 Python 代码来 运行。

将 CSV 文件写入我的桌面的 Python 代码。

import pandas as pd
string=['hello world','Sup']
DFstring=pd.DataFrame(string)
DFstring.to_csv("C:/Users/Peter_K/Desktop/test.csv")

我尝试使用 Excel 中的 VBA 代码调用此代码。

Sub xls2py()
    Dim objShell As Object
    Dim PythonExe, PythonScript As String
    ChDir ActiveWorkbook.Path

    Set objShell = VBA.CreateObject("Wscript.Shell")

    PythonExe = """C:\Users\Peter_K\anaconda3\python.exe"""
    PythonScript = "C:\Users\Peter_K\Desktop\Untitled3.py"

    objShell.Run PythonExe & PythonScript
    Set objShell = Nothing

End Sub

黑框命令提示框在屏幕上闪烁,但 CSV 不写入。

这里的问题有两个。首先,我没有在 python 脚本中导入 numpy。其次我没有正确设置我的路径。下面链接了一个关于如何执行此操作的最新视频。

https://www.youtube.com/watch?v=uOwCiZKj2rg&t=510s