[python-win32] com, CreateObject, and py2exe

Bill Purcell william at whpiv.net
Thu Feb 24 01:25:00 CET 2011


I am trying to expose a couple Numpy functions to excel.  Namley
Numpy.linalg.inv and Numpy.array.dot.  I have successfully accessed
the following com server from within excel with CreateObject

========================================================================
test_com.py
========================================================================
from numpy.linalg import inv
from numpy import asarray
from win32com.client import Dispatch


class NevcoModule:
    _public_methods_ = [ 'MInverse', 'MMult', "TestInstall" ]
    _reg_progid_ = "Nevco.Modules"
    _reg_clsid_ = "{79D06C97-331A-4BF8-824A-8DA3B618F8DB}"

    def TestInstall(self):
        return "Yes"

    def MInverse(self,arg):
        myarg = asarray(arg)
        i = inv(myarg)
        return i

    def MMult(self,M1,M2):
        mM1 = asarray(M1)
        mM2 = asarray(M2)
        x = mM1.dot(mM2)
        return x

if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.RegisterClasses(NevcoModule)
    interp = Dispatch("Nevco.Modules")
    raw_input("Leave running and then press enter to exit.")
    win32com.server.register.UnregisterClasses(NevcoModule)
========================================================================

I then have a function in VBA inside excel which accesses this server
with Obj = CreateObject("Nevco.Modules").  I would like to share this
code with a couple coworkers who are not interested in learning
python, so I would like to create an executable to start the server
for them.  My py2exe setup.py look like

========================================================================
setup.py
========================================================================
from distutils.core import setup
import py2exe

setup(
    name="Nevco Excel",
    version="0.1",
    description='Functions to use inside excel.',
    author='Bill Purcell',
    author_email='bpurcell at nevco.com',
    com_server=["win32com.servers.interp"],
    options = {'py2exe': {'includes': 'numpy'}},
)
========================================================================
 
Unfortunately when I run the test_com.exe generated from python
setup.py py2exe, I get a "Cannot Create Object" error.  I can't
remember the exact error but that is pretty close.  When I run the
function excel just hangs and a console displays with the text about
installing the "Nevco.Modules" object.  What is the right approach to
exposing com servers to be accessible via py2exe to VBA?

Also, I would like to eventually move towards maybe writing an
Add-in.  Does anyone have any "Hello World" excell Add-in tutorials
for Python?

I know these topics are slightly redundant in the list but I couldn't
find any good answers.

Regards,
Bill


More information about the python-win32 mailing list