【问题标题】:Passing in multiple arguments in VBA to run a Python script在 VBA 中传递多个参数以运行 Python 脚本
【发布时间】:2015-09-01 15:53:24
【问题描述】:

我有一个非常简单的 VBA 脚本,它将运行一个 Python 脚本,该脚本具有指定的参数(它们是文件夹路径,包含空格)。

代码如下

Dim path As String
path = Left(pdocinfo.path, InStrRev(pdocinfo.path, "\")) ' e.g C:\Temp\City Addresses

Dim pyLocation As String
pyLocation = "C:\Temp\filePath.py"

Dim strReports As String
strReports = path & "Reports\"

Dim strImages As String
strImages = path & "Images\"
Dim shellvar As Long
shellvar = shell("C:\Python26\ArcGIS10.0\python.exe " & pyLocation & " " & path & " " & strImages & " " & strReports , vbNormalFocus)

但是,当 Python 脚本运行时,文件路径(strImagesstrReportspath)无法正确解析,因为它们在文件夹路径中包含空格。

Python 解释多个作为动态变量且还包含空格的参数的正确方法是什么?

编辑: 从下面的评论开始,我尝试了以下方法

shellvar = Shell("C:\Python27\ArcGIS10.2\python.exe " & pyLocation & " " & """" & path & """" & " " & """" & strImages & """" & " " & """" & strReports & """", vbNormalFocus)

python 脚本的结果是

我想要的结果是

Argument 0: C:\Temp\filePath.py
Argument 1: "C:\Temp\City Addresses"
Argument 2: "C:\Temp\City Addresses\Images"
Argument 3: "C:\Temp\City Addresses\Reports"

【问题讨论】:

  • 你可能不得不用双引号括起来。 & """" & strImages & """" & 有四个双引号产生一个双引号。

标签: python vba quotes


【解决方案1】:

用双引号括起来。

shellvar = shell("C:\Python26\ArcGIS10.0\python.exe " & pyLocation & " " & """" & path & """" & " " & """" & strImages & """" & " " & """" & strReports & """", vbNormalFocus)

【讨论】:

  • 我试过了,但是python中使用的参数不正确。我更新了问题以显示您的示例以及结果的屏幕截图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 2011-08-31
相关资源
最近更新 更多