Clyde
Clyde

Reputation: 127

Running python from VBA word and returning a value

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)

Upvotes: 1

Views: 1126

Answers (1)

jackjr300
jackjr300

Reputation: 7191

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

Upvotes: 2

Related Questions