Calling Python functions from Excel

Discussion in 'Python' started by Cannonbiker, Nov 15, 2009.

  1. Cannonbiker

    Cannonbiker Guest

    Cannonbiker, Nov 15, 2009
    #1
    1. Advertising

  2. Carsten Haese, Nov 15, 2009
    #2
    1. Advertising

  3. Cannonbiker

    Darcy Mason Guest

    On Nov 15, 2:20 am, 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.


    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.
     
    Darcy Mason, Nov 16, 2009
    #3
  4. Chris Withers, Nov 17, 2009
    #4
  5. Cannonbiker

    Mark Tolonen Guest

    "Chris Withers" <> wrote in message
    news:...
    > Cannonbiker wrote:
    >> 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.

    >
    > 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
     
    Mark Tolonen, Nov 17, 2009
    #5
  6. 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
     
    Chris Withers, Nov 17, 2009
    #6
  7. Cannonbiker

    Mark Tolonen Guest

    "Chris Withers" <> wrote in message
    news:...
    > 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
     
    Mark Tolonen, Nov 18, 2009
    #7
  8. Cannonbiker

    Ethan Furman Guest

    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
     
    Ethan Furman, Nov 18, 2009
    #8
  9. Cannonbiker

    Cannonbiker Guest

    On 18 lis, 03:09, "Mark Tolonen" <> wrote:
    > "Chris Withers" <> wrote in message
    >
    > news:...
    >
    > > 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/

    >
    > >> 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


    Thanks very much. It works perfectly!!! :)
     
    Cannonbiker, Nov 18, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Vallstrom
    Replies:
    2
    Views:
    2,039
    Kevin Bracey
    Nov 21, 2003
  2. Mark Carter
    Replies:
    1
    Views:
    510
    Koczian
    Jul 11, 2003
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  4. Ross A. Finlayson
    Replies:
    19
    Views:
    650
    Keith Thompson
    Mar 10, 2005
  5. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,596
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
Loading...

Share This Page