win32 problem interacting with word97

Andrew Brown killspam at darwinwars.com
Wed Mar 27 07:22:06 EST 2002


I am trying to convert a couple of hundred MS word 97  files to html, and 
then reformat them into a template. 

Mostly, this works, but there are some which contain accented letters, 
and the Word html converter barfs on these in a peculiarly irritating 
way: it puts up a dialogue box with a message saying 

"This document contains characters that are not in the current language 
encoding. To preserve them, choose cancel and select the appropriate 
language encoding. For multilingual documents, use UTF-8."

It is possible to press "OK" or "cancel" or any damn thing if you can see 
the dialogue box; but my program can't see it, and goes on trying to get 
word to process the next file until, after about 30 seconds python 
crashes completely. Only then do I see the dialogue box which has caused 
the trouble. 

What I would like to be able to do is to get around this. Since it's not, 
so far as Word is concerned, an error, no exception appears. So I can't 
catch it that way. I don't know any way to detect that Word expects 
input. Is there one? 

The crudest thing that occurs to me is simply to send an "esc" character 
(to dismiss the dialogue box) after every file has been processed. But I 
don't know how to do that, even. 

Is there some kind of "sendkeys" method?

In case it's enlightening, here is the bit of code where everything goes 
wrong:

def visitfile(self,filepath):
    if string.find(filepath, 'doc')>1:
      htmfile=os.path.splitext(filepath)[0]+'.htm'
      print 'htmfile is %s' %htmfile
      try:
        self.o.Documents.Add(os.path.abspath(filepath))
        self.o.ActiveDocument.SaveAs(os.path.abspath(htmfile),17)
        self.o.ActiveDocument.Close()
      except:
        warning='MS word convert failed ' + str(sys.exc_type) + str\ 
(sys.exc_value)
        print 'filepath is %s' %filepath
        print warning
      # but there is no exception, so the dialogue box goes unnoticed
      .....





More information about the Python-list mailing list