Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling Python functions from Excel

66 views
Skip to first unread message

Cannonbiker

unread,
Nov 15, 2009, 2:20:35 AM11/15/09
to
Hi,
unfortunately is my question about server COM (win32com)
http://groups.google.com/group/comp.lang.python/browse_thread/thread/ee804cec7f58c6a7#
without answer.

Please I need Calling Python functions from Excel and receive result
back in Excel. Can me somebody advise simplest solution please? I am
more VBA programmer than Python.

Thanks

Carsten Haese

unread,
Nov 15, 2009, 2:53:49 AM11/15/09
to pytho...@python.org
Cannonbiker wrote:
> Please I need Calling Python functions from Excel and receive result
> back in Excel. Can me somebody advise simplest solution please? I am
> more VBA programmer than Python.

Maybe this will help:
http://oreilly.com/catalog/pythonwin32/chapter/ch12.html (Scroll down to
"Implementing a COM Server.")

--
Carsten Haese
http://informixdb.sourceforge.net

Darcy Mason

unread,
Nov 16, 2009, 8:34:27 AM11/16/09
to
On Nov 15, 2:20 am, Cannonbiker <lusve...@gmail.com> wrote:

> Please I need Calling Python functions from Excel and receive result
> back in Excel. Can me somebody advise simplest solution please? I am
> more VBA programmer than Python.

A couple of years ago I used MSScriptControl for this. Couldn't find a
great reference just now, but here is a discussion which should give
enough information:
http://www.velocityreviews.com/forums/t319222-re-python-in-excel.html

Check from around message 3 on.

Chris Withers

unread,
Nov 17, 2009, 6:36:33 AM11/17/09
to Cannonbiker, pytho...@python.org

Mark Tolonen

unread,
Nov 17, 2009, 11:23:39 AM11/17/09
to pytho...@python.org

"Chris Withers" <ch...@simplistix.co.uk> wrote in message
news:4B028AC1...@simplistix.co.uk...
> Try http://code.google.com/p/pyinex/

The book Python: Programming on Win32 has a whole chapter on COM, and a
section on COM servers.

-Mark


Chris Withers

unread,
Nov 17, 2009, 11:40:03 AM11/17/09
to Mark Tolonen, pytho...@python.org
Mark Tolonen wrote:
>
>>> Please I need Calling Python functions from Excel and receive result
>>> back in Excel. Can me somebody advise simplest solution please? I am
>>> more VBA programmer than Python.
>>
>> Try http://code.google.com/p/pyinex/
>
> The book Python: Programming on Win32 has a whole chapter on COM, and a
> section on COM servers.

...and it's generally accepted that COM sucks rocks through straws, so
explore alternatives when they're available ;-)

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Mark Tolonen

unread,
Nov 17, 2009, 9:09:13 PM11/17/09
to pytho...@python.org

"Chris Withers" <ch...@simplistix.co.uk> wrote in message
news:4B02D1E3...@simplistix.co.uk...

> Mark Tolonen wrote:
>>
>>>> Please I need Calling Python functions from Excel and receive result
>>>> back in Excel. Can me somebody advise simplest solution please? I am
>>>> more VBA programmer than Python.
>>>
>>> Try http://code.google.com/p/pyinex/
>>
>> The book Python: Programming on Win32 has a whole chapter on COM, and a
>> section on COM servers.
>
> ...and it's generally accepted that COM sucks rocks through straws, so
> explore alternatives when they're available ;-)
>
> Chris

True, but as usual Python makes it pretty darn easy (requires PyWin32):

------------- ex.py -------------------------------
class Example(object):
_public_methods_ = ['Add','Mul']
_reg_progid_ = 'MyPython.Example'
_reg_clsid_ = '{insert_GUID_here}'

def Add(self,a,b):
return a+b

def Mul(self,a,b):
return a*b

if __name__ == '__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(Example)
---------------------------------------------------------

-------------- Excel Macro ----------------------
Sub Testit()
Set ex = CreateObject("MyPython.Example")
Range("A1") = ex.Add(1, 2)
Range("A2") = ex.Mul(3, 4)
End Sub
--------------------------------------------------------

Just run the script to register the server. "ex.py --unregister" will
remove it.

-Mark


Ethan Furman

unread,
Nov 17, 2009, 9:33:21 PM11/17/09
to pytho...@python.org
Chris Withers wrote:

> Mark Tolonen wrote:
>> The book Python: Programming on Win32 has a whole chapter on COM, and
>> a section on COM servers.
>
> ...and it's generally accepted that COM sucks rocks through straws, so
> explore alternatives when they're available ;-)

+1 QOTW :D

Cannonbiker

unread,
Nov 18, 2009, 1:38:19 PM11/18/09
to
On 18 lis, 03:09, "Mark Tolonen" <metolone+gm...@gmail.com> wrote:
> "Chris Withers" <ch...@simplistix.co.uk> wrote in message
>
> news:4B02D1E3...@simplistix.co.uk...
>
> > Mark Tolonen wrote:
>
> >>>> Please I need Calling Python functions from Excel and receive result
> >>>> back in Excel. Can me somebody advise simplest solution please? I am
> >>>> more VBA programmer than Python.
>
> >>> Tryhttp://code.google.com/p/pyinex/

Thanks very much. It works perfectly!!! :-)

0 new messages