Description

Typically when dealing with 3rd party COM libraries (eg. Office), you make use of the constants defined in that library. Once makepy creates the typelib for you, using these constants is normally easy in python. You only need to import win32com.client.constants to access them.

However, since these typelibs aren't really imported, py2exe needs to be told to include them in your setup-script.

Solution 1

Here's how I'm importing the typelib for Excel XP:

setup.py:

   1  ...
   2  setup(
   3         ...,
   4         options = {"py2exe": {"typelibs": [('{00020813-0000-0000-C000-000000000046}',0,1,4)]}},
   5         ...
   6       )

So, as you can see, it's an option in the options dictionary, containing a list of the typelibs you need. Each typelib being represtented as a tuple of (CLSID, LCID, MajorVersion, MinorVersion) - all of which numbers you can find in the typelib file itself.

You can print out these magic numbers by running the makepy script with the -i command line option.

Solution 2

That solution was the easier one for py2exe 0.4, but it still works:

cd \python23\Lib\site-packages\win32com\client
python makepy.py -o {MyProjectDirectory}\OLE_Excel10.py

within the software change:

   1 import win32com.client
   2 o = win32com.client.Dispatch("Excel.Application")
   3 o.Method()
   4 o.property = "New Value"
   5 print o.property

(taken from M. Hammonds documentation "Quick Start to Client side COM and Python")

to

   1 import OLE_Excel10 as Excel
   2 _ec=Excel.constants
   3 
   4 o = Excel.Application()
   5 o.Method()
   6 o.property = "New Value"
   7 print o.property

I believe that Solution 2 is not correct. It will work if you happen to be on a machine that has python installed, but if your goal is to use py2exe to distribute an application to machines that don't have python (and win32com) installed, then I don't think it will work. What the py3exe stuff in Solution 1 does is incorporate the generated win32com wrappers into the py2exe stuff. -Alec Wysoker