Which way is best to execute a Python script in Excel?
Emile van Sebille
emile at fenx.com
Thu Jul 5 10:28:39 EDT 2012
On 7/5/2012 12:22 AM Maurizio Spadaccino said...
> Hi all
>
> I'm new to Python but soon after a few days of studying its features I
> find it my favourite mean of programming scripts to allow for data
> storing and mining. My idea would be to inplement python scripts from
> inside an excel sheet that would store and fetch data from a Mysql
> database.
Look again at the com interface -- I've created excel commands
implemented entirely in python this way and it will do everything you're
looking for. Just start with some of the examples and extend from there.
See http://oreilly.com/catalog/pythonwin32/chapter/ch12.html
I did this ten years ago so things have likely changed, but the bits of
the python script that set up com then looked like this:
class fenxUtilities:
_public_methods_ = [ 'FirstPartInsp' ]
_reg_progid_ = "fenxDCom.Util"
_reg_clsid_ = "{3EAD7AB4-2978-4360-8F7D-33FB36E9E146}"
def FirstPartInsp(self,nomDiam, numFlutes, nomOAL, nomLOC):
return EMSpecs(nomDiam, numFlutes, nomOAL, nomLOC).retvals
if __name__=='__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(fenxUtilities)
HTH,
Emile
> So i need the script to be launched, say, by pressing a button
> in excel and, for instance, retrieve immediately data from the mysql
> table. For what I found in the net, it seems there are three ways to
> control python from excel:
> 1) run my python script via shell using the Run command in VBA, which
> seems to me the easiest (but maybe slower?) way;
> 2) creating a COM server, for which I need more training since it doesnt
> appear that easy;
> 3) via Microsoft Script Control, for which I saw some example around
> where It looks like I can 'simulate' a python shell via the
> 'executeStatement' command.
>
> What would you suggest would be the more performing way to achieve my
> goals as described above?
>
> Thanks for you help
> Maurizio
>
>
> --
> Maurizio
>
>
> www.mauriziospadaccino.com <http://www.mauriziospadaccino.com>
> ---------------
> If you can avoid printing this e-mail, you are only doing good for our
> planet.
>
>
More information about the Python-list
mailing list