1

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)
6
  • Might be worth for you having a look at this project. Commented Jan 16, 2015 at 16:07
  • This requires you to download "ExcelPython", and I'm trying to write some code that requires no downloading. Just the original word template I'm making. Commented Jan 16, 2015 at 16:16
  • 1
    Then I might suggest you to: 1) create a file with VBA and place it into the current directory where the code runs; 2) fill it with your python strings; 3) save it as ".py" extension; 4) use the script you have where you can call an existing python file; 5) run it and get the result; 6) close it and destroy it from the current directory. But if all the other people have a python interpreter installed (cause I guess they have, if they will be able to execute Python from their pc), can't you just distribute the installer along with your file? Commented Jan 16, 2015 at 16:22
  • Everyone I'm send the template to already have python installed on their computer. Let me see if I can try and implement your idea and I'll comment back. Commented Jan 16, 2015 at 16:26
  • If you succeed writing the code and it works fine, please post it as an answer to your post. It will be of help to users that might be in your same situation. Commented Jan 16, 2015 at 16:29

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

This looks promising, hopefully my mac will start working again and I can test. If not today I'll be able to Monday and will let everyone know if it worked for me.
I was about to suggest using macscript to do shell a python call. This is the simplest way to go.
When I try to run the script it returns: run-time error '5': invalid procedure call or argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.