problem controlling MS Word from Python
Andrew Brown
killspam at darwinwars.com
Fri Oct 5 08:38:45 EDT 2001
I am ignorant and inexerienced, so I may have missed something
obvious. Or I may have found a real bug. I have been using Python on Windows
2000 to process a bunch of word documents, first saving them as html using
Word itself as the translator, then removing all the Microsoft cruft from
the resulting files; finally reformatting them into my preferred layout.
The problem I am worried about comes with the first step: when Word stops
converting a document, complaining about the code page being wrong, the
whole Pythonwin program (ActiveState 2.1) crashes too. Yet I thought this is
what exceptions were meant to catch.
Here are the relevant bits of code:
def domydir(self):
"Processes all the .doc files in a directory using do_that_thing()"
QApplication.setOverrideCursor(Qt.waitCursor)
try:
self.o=win32com.client.Dispatch('Word.Application')
self.o.Visible=1
except:
warning='OLE fuckup! ' + str(sys.exc_type) + str(sys.exc_value)
self.btn_dirpick.setText(warning)
mypath,myfile=os.path.split(str(self.wombat_txt))
os.chdir(mypath)
self.o.ChangeFileOpenDirectory(mypath)
# show what we're doing on the button
for wombats in os.listdir(os.getcwd()):
if string.find(wombats, 'doc')>1:
htmlfile=self.htmlext(wombats)
self.do_that_thing(wombats,htmlfile)
print 'have just processed', htmlfile
QApplication.restoreOverrideCursor()
And
def do_that_thing(self,docfile,htmlfile):
'''gets an instance of word, to save the docfile as html.'''
try:
self.o.Documents.Add(docfile)
self.o.ActiveDocument.SaveAs(htmlfile,17)
self.o.ActiveDocument.Close()
except:
warn='DO that Thing! ' + str(sys.exc_type) + str(sys.exc_value)
self.btn_dirpick.setText(warn)
I'd have thought that, since all the calls to Word are wrappred in "try"
blocks, there should not be a problem when Word throws up a dialogue box.
But there is. The whole thing crashes. Does anyone know why this happens, or
how to avoid it?
More information about the Python-list
mailing list