通过VBA运行Python脚本并提供活动工作簿的名称和路径?

问题描述:

编辑:这是同一个问题,但我重写了它,所以它更清晰。通过VBA运行Python脚本并提供活动工作簿的名称和路径?

我已经尝试过这个帖子: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文件没有家庭地址(可以在任何桌面上)。代码I(想要)使用:

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“”,否则它会出错。

我想知道是否可以将活动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

+0

感谢您提供这些提示。我明白我的问题有点含糊,但在问这个问题之前,我真的先看看。我从来没有听说过壳。所以谢谢,我会查找它! – VeVi

+0

如果您真的显示了VBA代码,那么协助您会更容易。因为我们必须猜测你想要做什么,并且由于VBA中没有'RetVal'函数,所以我不得不假设你正在使用一个常见的结构,比如'RetVal = Shell(“一些shell命令“)'。如果您还没有以某种方式使用'Shell',那么我不知道您正在尝试执行什么操作,或者您如何从VBA调用Python对象。 [请通过修改您的问题进一步解释](http://*.com/posts/40657992/edit),如果这不能解决您的问题。 –

是的,我发现问题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瞧这个信息。它工作!那么它做我想要的,但它不是一个干净的解决方案。如果有人知道正确的解决方案,请随时分享: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) 

代码蟒蛇:

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")