Running python from VBA word and returning a value

◇◆丶佛笑我妖孽 提交于 2019-12-23 01:44:12

问题


Trying to figure out how to open python from word vba, on mac computers, and return a value using the code below. I need to be able to run this code from word vba without calling a specific python file, this will be run on multiple computers so I can't assume they have my premade python file downloaded. All I've seen is people calling a premade file with the script already in it.

Here is my Python Code:

import urllib2
web1 = urllib2.urlopen('website.com') #website url
print(web1.read())

Any suggestions or links to a place that could help?

Edit:

Current Working Code

myAppleScript = "do shell script ""/usr/bin/python -c 'import urllib2; web1=urllib2.urlopen(\""http://website.com\""); print(web1.read())' """
Updated = MacScript(myAppleScript) 'Retrieves the source from an URL

The Code above saves the source code to the variable "Updated" for later use.

Thanks for all the help and I hope this will also be useful to others, I know I'll probably be using it more in the future.

Edit:

Just so people can have it, here is some code similar that instead sends the user to a website.

myAppleScript = "do shell script ""/usr/bin/python -c 'import webbrowser; webbrowser.open_new(\""http://http://website.com\"")' """
MacScript (myAppleScript)

回答1:


You can use the MacScript function to run a do shell script command.

VBA code example:

myAppleScript = "do shell script ""/usr/bin/python -c 'import urllib2; web1=urllib2.urlopen(\""http://www.apple.com\""); print(web1.read())' """
htmlText = MacScript(myAppleScript) 'get the source from an URL


来源:https://stackoverflow.com/questions/27986516/running-python-from-vba-word-and-returning-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!