4

I have comtypes 0.6.2 installed on Python 2.6. If I try this:

import comtypes.gen

I get:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import comtypes.gen
ImportError: No module named gen

Other imports like import comtypes and import comtypes.client, however, work fine.

What am I doing wrong?

From the name it seems comtypes.gen is generated code? If so, do I need certain preparatory steps before importing? I'm not logged in as administrator. Could that cause code generation to fail?

Edit: The above problem is solved with a reload(comtypes.gen) (I don't understand how, though). However, now from comtypes.gen import IWshRuntimeLibrary is not working. This symbol should be part of a generated code. So how do I get this code to be generated?

G S
  • 35,511
  • 22
  • 84
  • 118

3 Answers3

5

Well, after some experimentation, I have the solution.

I've found that:

  1. Importing comtypes.client automatically creates the comtypes.gen subpackage.
  2. Calling comtypes.client.GetModule("MyComLib") generates a wrapper for "MyComLib".

So the following code did the job for me:

import os
import glob 
import comtypes.client

#Generates wrapper for a given library 
def wrap(com_lib): 
    try: 
         comtypes.client.GetModule(com_lib) 
    except: 
         print "Failed to wrap {0}".format(com_lib) 

sys32dir = os.path.join(os.environ["SystemRoot"], "system32") 

#Generate wrappers for all ocx's in system32 
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")): 
    wrap(lib) 

#Generate for all dll's in system32 
for lib in glob.glob(os.path.join(sys32dir, "*.tlb")): 
    wrap(lib) 

Having the relevant COM lib wrapped, now I can access IWshRuntimeLibrary just fine.

G S
  • 35,511
  • 22
  • 84
  • 118
1

Perhaps, as it says, the package gen package in comptypes doesn't exist. Check your site-packages folder (C:\Python26\Lib\site-packages on Windows, substitute the C:\Python26 with your installation directory) for a comtypes\gen subfolder.

Peter C
  • 6,219
  • 1
  • 25
  • 37
  • The `gen` sub-folder, including its proper `__init__.py`, does exist in my site-packages. So, since I'm able to import `comtypes`, I should be able to import this particular sub-package too. – G S Oct 10 '10 at 02:18
  • Update: I just did a `reload(comtypes.gen)`, and, miraculously, it worked. Now, next problem: I'm trying to import a symbol using `from comtypes.gen import IWshRuntimeLibrary`. I should be able to do this (because this line is from the code of DreamPie editor). But it fails. But then that's expected because the `__init__.py` mentioned above is completely empty and comments inside it say something about generated code. So my question is, how do I get the code to generate (for the lack of a better phrasing)? – G S Oct 10 '10 at 02:26
  • Well that's unusual. Perhaps there is a syntax error in __init__.py? EDIT: Oh. Maybe there is a script to run somewhere to generate the code? And is the code supposed to be generated automatically at runtime? BTW, are there any other files besides __init__.py in the gen folder? – Peter C Oct 10 '10 at 02:27
  • No, it's just the `__init__.py` and its compiled version. Let me scour the comtypes install folder for any scripts. – G S Oct 10 '10 at 02:31
  • I've found a codegenerator.py, but am really not sure how to get it working. Ideally, one would expect it should run automatically at install time or some automagic of that sort. Anyway, this is now gonna need some tinkering. Thank you alpha123 for nudging me in the right (although, what should have been obvious) direction. – G S Oct 10 '10 at 02:42
  • No problem. Looking at your own answer below, glad to see you got it working. – Peter C Oct 11 '10 at 00:03
0

recently i got the new office, and i had to extend the script of @frederick to generate all the office objects again.

import os
import glob
import comtypes.client
# You may want to change the office path
msoffice=r'C:\Program Files (x86)\Microsoft Office\root\Office16'

#Generates wrapper for a given library
def wrap(com_lib):
    try:
         comtypes.client.GetModule(com_lib)
    except:
         print("Failed to wrap {0}".format( com_lib))

sys32dir = os.path.join(os.environ["SystemRoot"], "system32")

#Generate wrappers for all ocx's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")):
    wrap(lib)

#Generate for all dll's in system32
for lib in glob.glob(os.path.join(msoffice, "*.tlb")):
    wrap(lib)

for lib in glob.glob(os.path.join(msoffice, "*.olb")):
    wrap(lib)

# And a special case for Excel
excel=os.path.join(msoffice,"excel.exe")
wrap(excel)
Mandragor
  • 4,684
  • 7
  • 25
  • 40