Ich bin python einsteiger und schreibe unter win ein office-zeugs konverter, dazu nutze ich eclipse + python (pydev).
Es geht bei meiner frage nicht um pythoncom spezifische eigenschaften, das problem steckt eher in den threads.
Dazu: Ich habe eine klasse word welche von officeapplication erbt welche wiederum von othread(Thread) erbt.
Die inits sehen wie folgt aus:
Code: Alles auswählen
class Word(office.Officeapplication):
"""A utility to make it easier to get at Ms Word."""
def __init__( self, filename=None, printer=None ):
...
self.event = WordEvent
_Debug.debug( "init word instance" )
super( Word, self ).__init__( 'Word.Application', self.event, filename, printer )
...
Code: Alles auswählen
class Officeapplication(OThread):
'''
class-constructor'''
def __init__(self, name, event=None, filename=None, printer=None):
super( Officeapplication, self ).__init__(None, None, name)
Code: Alles auswählen
# othread.py
from threading import Thread
import pythoncom
class OThread(Thread):
def __init__(self, group=None, target=None, name=None):
print "thread init called"
Thread.__init__(self, group, target, name)
print "mashall com for thread %s " % self.getName()
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
def run(self):
print "Implement me"
def __del__(self):
pythoncom.CoUninitialize()
Code: Alles auswählen
def run(self):
_Debug.debug("thread '%s' startet ..." % self.getName())
before = []
after = dict ([(f, None) for f in os.listdir (self.config['directory_in'])])
added = [f for f in after if not f in before]
for fn in added:
if fn[-3:] in self.extensions:
starttime = time.clock()
self.printout(self.config['directory_in'] + "\\" + fn)
while self.App.BackgroundPrintingStatus > 0:
time.sleep(0.1)
Daraus schliesse ich, dass fuer den thread mit namen 'Word.application' nicht die CoInit methode aufgerufen worden ist.creating a word com client object
init word instance
officeapplication inheritances
thread init called
mashall com for thread Word.Application
setting up eventhandler
thread 'Word.Application' startet ...
finalize
Word.Application
Exception in thread Word.Application:
Traceback (most recent call last):
File "C:\Python24\lib\threading.py", line 442, in __bootstrap
self.run()
File "e:\eclipse\workspace\Office2pdf\src\office.py", line 141, in run
self.printout(self.config['directory_in'] + "\\" + fn)
File "e:\eclipse\workspace\Office2pdf\src\word.py", line 126, in printout
for doc in self.App.Documents:
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 199, in __getattr__
return getattr(self._obj_, attr)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 455, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 446, in _ApplyTypes_
return self._get_good_object_(
com_error: (-2147221008, 'CoInitialize wurde nicht aufgerufen.', None, None)
Anhand der ausgaben sieht man aber, dass dies dennoch geschehen ist.
Was mache ich falsch ?
p.s. die ref. der CoInit methode http://aspn.activestate.com/ASPN/docs/A ... _meth.html