使用excel VB Shell函数执行python代码不会在python指令处停止。它可以快速打开和关闭,但不显示Python指令。以下excel宏运行python代码:
Sub run_python()
python_exe = "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe"
python_script = "C:/Users/Goerge/OneDrive - MS Corporation/Documents/Development/python-excel/excel_python.py"
If Dir(python_exe) = "" Then
MsgBox ("Python executable does not exists")
End If
If Dir(python_script) = "" Then
MsgBox ("Python script does not exists")
End If
python_code = python_exe & " " & " -k " & python_script
RetVal = Shell(python_code)
End Sub
python代码示例:
import os
print("This is first python function called in excel")
for x in range(0,9999999999999999999):
print("Counting : ", x)
#input('Press ENTER to exit')
os.system("pause")
我在这里添加了几个步骤(用于循环和“暂停”)来保持代码的执行,但在通过宏执行时它不会响应。然而,当直接在VS代码中运行时,该代码可以正常工作。
我做错了什么?
我使用的是Excel 365和Python 3.7。