首页 > 解决方案 > Python 脚本从命令行运行,从 Excel Shell 停止工作

问题描述

我有一个 Python 脚本,当从远程服务器上的 Excel 执行时,它已成功运行。我最近在本地 PC 上进行了一些测试,更新了 Python 脚本并通过远程桌面进行了测试。从那时起,在远程桌面上,Python 脚本在从命令行执行时工作,但在 Excel 中执行相同的命令时停止工作。代码示例:

'Get file from Gmail using a Python script
vPytnonDir = "C:\Projects\Python"
ChDir vPytnonDir  'Directory where python script resides
Dim wShell As Object
Set wShell = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wShell.Run "cmd.exe /S /C python GetGmail.py", windowStyle, waitOnReturn

当我在命令窗口(在 Python 目录中)运行“cmd.exe /S /C python GetGmail.py”时,该命令有效。我已经刷新了 credentials.json 和 token.json 以拥有对 Gmail 的相同用户访问权限。我在 Python 脚本中暂停了,执行窗口只是闪烁,显然脚本没有暂停。我只用示例代码创建了一个独立的 Excel - 并且有效!这让我发疯(不能说疯了,已经在那里) - 任何帮助将不胜感激。

这已经解决了!当我从本地 PC 完成测试后,我通过“E”驱动器连接到服务器,并且不知何故重置了 Excel 的默认驱动器。代码中的 ChDir 命令根本不起作用,我必须运行 ChDrive("C") 才能让事情恢复正常。

标签: pythonjsonexcelcmdgmail

解决方案


推荐阅读