【问题标题】:Run Python script via VBA and give name and path of active workbook?通过 VBA 运行 Python 脚本并给出活动工作簿的名称和路径?
【发布时间】:2017-04-01 04:16:07
【问题描述】:

编辑:这是同一个问题,但我重写了它,所以它更清晰。

我已经尝试过这个帖子:How to call python script on excel vba?

还有这个帖子:Run and execute a python script from VBA

还有这个帖子:How can I call python program from VBA?

但是没有一个答案对我有用,我不知道我做错了什么。问题 1:我想从 VBA excel 运行 pythonscript。 excel 文件没有归宿(可以在任何桌面上)。我(想要)使用的代码:

 Dim Ret_Val
 Ret_Val = Shell("C:\python27\python.exe \\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\DescriptionToDatabase.py")

pythonfile 在服务器上始终具有相同的路径。我看不出这里有什么问题吗?我得到的只是一个黑色的蟒蛇屏幕。

在 python 文件中,我调用了工作簿和正确的工作表:

book = xlrd.open_workbook("//10.31.13.22/SharedDocs/3 - Technical/1 - Projects/0 - Internal/RPA 138 - Engineering software/testchipdescription/upload to database/testchipdescription-template-10-11.xltm")
sheet = book.sheet_by_name("Database")

目前,excel 工作簿路径在 python 中是硬编码的。这会让我回到问题 2:我可以以某种方式将 excel 工作簿的名称和路径传递给我的 pythonscript 吗?

编辑:

我在命令提示符下尝试了 shell() 代码。 与 VBA 相同:

"C:\python27\python.exe \\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\DescriptionToDatabase.py"

它不起作用。 '系统找不到指定的路径'。

我试过这个:

C:\python27\python.exe "\\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\DescriptionToDatabase.py"

而且它有效!所以cmd需要""来处理路径中的空格。但是我不能在 VBA 中添加它们,因为我不能放置 2 "" 否则会出错。

【问题讨论】:

    标签: python vba excel


    【解决方案1】:

    是的,我找到了问题 1 的解决方案:

    Dim excelToPython As String
    excelToPython = """C:\python27\python.exe"" ""\\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\DescriptionToDatabase.py"""
    Debug.Print excelToPython
    Call Shell(excelToPython)
    

    感谢“艾伦·布朗”(https://bytes.com/topic/access/answers/520558-problem-shell-space-file-name)

    编辑:

    最后我找到了问题 2 的解决方法。我仍然没有真正的解决方案来使用 shell 命令将活动工作簿的名称和路径提供给我的 python 脚本。

    但我将活动工作簿的路径和名称写入我的 python 脚本所在文件夹中的 txtfile 中。然后我用我的 pythonscript et voila 得到这个信息。它有效!好吧,它可以满足我的要求,但这不是一个干净的解决方案。如果有人知道正确的解决方案,请随时分享:o)

    我的解决方法:

    vba-excel 中的代码:

    'via pythonscript'
    
    Dim excelToPython As String
    Dim myFileTxt As String
    Dim fileTxtPath As String
    
    'first give the name and path of active workbook to a txtfile in the same folder as ThemeColor pythonscript'
    
    fileTxtPath = "\\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\actWBdb.txt"
    
    myFile = FreeFile
    
    Open fileTxtPath For Output As myFile
    
    nameWB = ActiveWorkbook.name
    pathWB = ActiveWorkbook.path
    
    Print #myFile, nameWB
    Print #myFile, pathWB
    
    Close myFile
    
    'run the python file'
     excelToPython = """C:\python27\python.exe"" ""\\10.31.13.22\SharedDocs\3 - Technical\13 - Reports & Templates\13 - Description\DescriptionToDatabase.py"""
    
    Call Shell(excelToPython)
    

    python中的代码:

    filepath ='//10.31.13.22/SharedDocs/3 - Technical/13 - Reports & Templates/13 - Description/actWBdb.txt'
    lines =  open(filepath).read().splitlines()
    nameWorkbook = lines[0]
    pathWorkbook = lines[1]
    
    book = xlrd.open_workbook(pathWorkbook + '/' + nameWorkbook)
    sheet = book.sheet_by_name("Database")
    

    【讨论】:

      【解决方案2】:

      我想知道是否可以将活动 Excel 工作簿的名称和路径提供给我从该活动工作簿调用的 python 脚本?

      是的。 You can pass argument(s) to SHELL function from VBA,然后你需要修改你的 python 脚本来接受参数并用它/它们做一些事情。

      Python Read from Stdin with Arguments

      Python read from command line arguments or stdin

      【讨论】:

      • 感谢您提供这些提示。我知道我的问题有点含糊,但在我问这个问题之前我真的先看看。我从来没有听说过贝壳。所以谢谢,我会查一下!
      • 如果您实际展示了您的 VBA 代码,会更容易为您提供帮助。照原样,我们必须猜测您要做什么,并且由于 VBA 中没有 RetVal 函数,我不得不假设您正在使用像 RetVal = Shell("some shell command") 这样的通用构造。如果您还没有以某种方式使用Shell,那么我不知道您要做什么,或者您如何从 VBA 调用 python 对象。 Please explain further by making revision to your question,如果这不能解决您的问题。
      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多