Python in Excel

Afanasiy abelikov72 at
Fri Jul 11 20:08:57 CEST 2003

On 11 Jul 2003 08:16:33 -0700, ofnap at (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

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.


More information about the Python-list mailing list