Python in Excel

Afanasiy abelikov72 at hotmail.com
Fri Jul 11 14:08:57 EDT 2003


On 11 Jul 2003 08:16:33 -0700, ofnap at nus.edu.sg (Ajith Prasad) wrote:

>This looks very useful to Excel users who would like to tap the power
>of Python. However, not knowing enough about VBA syntax it was not
>possible to  proceed beyond step(a). Is it possible to provide an
>Idiot's guide to steps (b), (c),(d) and (e)? In other words, what is
>the explicit VBA code/steps need to do (b) and (c) and could simple
>(even trivial) examples be given of steps (d) and (e). Thanks in
>advance.

Global sc As New MSScriptControl.ScriptControl

Public Function os_getcwd()
    sc.Language = "python"
    sc.ExecuteStatement ("import os")
    os_getcwd = sc.Eval("os.getcwd()")
End Function

With this you can set your Excel formula to =os_getcwd()

For me it returns "C:\Documents and Settings\Administrator\My Documents",
which I needed to know at the time so I didn't have to screw around with
the ever annoying pythonpath.

You can put the first two lines of the function in the Workbook_Open hook,
but I don't know where that is. I hope to use more Python in Excel soon.
Hmm, actually, I suppose you can put those first two lines of the function
after the Global declaration as well. I know just about zero VBScript and
didn't get a chance to do anything else beyond proof of concept yet.

I figured I would write something dynamic which allowed more transparent
access to Python, maybe allowing formula like =py("os.getcwd()"), etc.

-AB




More information about the Python-list mailing list