[Zope-dev] COM

Jim Sanford jsanford@atinucleus.com
Tue, 11 Apr 2000 18:02:03 -0500


Have managed to get by the CoInitialize problem using COM with Zope in an external method. The method has been tested in straight
python 1.52 and works great there.

Now I need some help. Here is the traceback:
----------------------------------------------------------------------
...
 Error type:  AttributeError
 Error value: GetModuleForCLSID
...
Traceback (innermost last):
  File C:\Program Files\AtiAnywhere\lib\python\ZPublisher\Publish.py, line 214, in publish_module
  File C:\Program Files\AtiAnywhere\lib\python\ZPublisher\Publish.py, line 179, in publish
  File C:\Program Files\AtiAnywhere\lib\python\Zope\__init__.py, line 202, in zpublisher_exception_hook
    (Object: ElementWithAttributes)
  File C:\Program Files\AtiAnywhere\lib\python\ZPublisher\Publish.py, line 165, in publish
  File C:\Program Files\AtiAnywhere\lib\python\ZPublisher\mapply.py, line 160, in mapply
    (Object: AWordTest)
  File C:\Program Files\AtiAnywhere\lib\python\ZPublisher\Publish.py, line 102, in call_object
    (Object: AWordTest)
  File C:\Program Files\AtiAnywhere\lib\python\OFS\DTMLMethod.py, line 145, in __call__
    (Object: AWordTest)
  File C:\Program Files\AtiAnywhere\lib\python\DocumentTemplate\DT_String.py, line 502, in __call__
    (Object: AWordTest)
  File C:\Program Files\AtiAnywhere\lib\python\DocumentTemplate\DT_Let.py, line 145, in render
    (Object: TFile="r'q:\atidb\users\u\wwwTestTmplt.doc'"
          RFile="r'q:\atidb\users\u\Test19.doc'"
          RList="[['<Firstname>','Duanne'],['<LastName>','Jones']]")
  File C:\Program Files\AtiAnywhere\lib\python\DocumentTemplate\DT_Util.py, line 335, in eval
    (Object: WReplace(TFile,RFile,RList))
    (Info: RList)
  File <string>, line 0, in ?
  File C:\Program Files\AtiAnywhere\lib\python\Products\ExternalMethod\ExternalMethod.py, line 248, in __call__
    (Object: WReplace)
    (Info: (('q:\\atidb\\users\\u\\wwwTestTmplt.doc', 'q:\\atidb\\users\\u\\Test19.doc', [['<Firstname>', 'Duanne'],
['<LastName>', 'Jones']]), {}, None))
  File C:\Program Files\AtiAnywhere\Extensions\WordR.py, line 32, in WordReplace
  File C:\Program Files\AtiAnywhere\Extensions\WordR.py, line 23, in formletter
  File C:\Program Files\AtiAnywhere\Extensions\WordR.py, line 9, in __init__
  File C:\Program Files\Python\win32com\client\__init__.py, line 28, in Dispatch
AttributeError: (see above)
-----------------------------------------------------------------------------
WordR is reproduced below. It works fine in regular Python:

import os, sys, pickle
from client import constants, Dispatch
WORD = 'Word.Application.8'
False, True = 0, -1
import string

class Word:
    def __init__(self):
        self.app = Dispatch(WORD)
    def open(self, doc):
        self.app.Documents.Open(FileName=doc)
    def replace(self, source, target):
        find = self.app.Selection.Find
        find.Text = source
        self.app.Selection.Find.Execute()
        self.app.Selection.TypeText(Text=target)
    def printdoc(self):
        self.app.Application.PrintOut()
    def close(self):
        self.app.ActiveDocument.Close(SaveChanges=True)

def formletter(doc,rplctuple):
    word = Word()
    word.open(doc)
    for x in rplclist:
        word.replace(x[0],x[1])
    word.close()

def WordReplace(TmpltFile,ReplaceFile,ReplaceList):
    from shutil import copyfile
    copyfile(TmpltFile,ReplaceFile)
    formletter(ReplaceFile,ReplaceList)

# WordReplace(r"q:\atidb\users\u\wwwTestTmplt.doc", r"q:\atidb\users\u\Test10.doc",
[['<FirstName>','Jim'],['<LastName>','Sanford']])