problem controlling MS Word from Python 
Author Message
 problem controlling MS Word from Python

        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 {*filter*}up! ' + 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?



Tue, 23 Mar 2004 20:38:45 GMT  
 problem controlling MS Word from Python

Quote:
> 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?

Can you clarify what you mean by "crashes"?  Does python return a traceback,
or does the python interpreter itself crashes?  I wouldn't be surprised if
it was the later:  I've noticed Office apps connected by automation can
crash the client.


Tue, 23 Mar 2004 22:52:06 GMT  
 problem controlling MS Word from Python


Quote:
>Can you clarify what you mean by "crashes"?  Does python return a
>traceback, or does the python interpreter itself crashes?  I wouldn't be
>surprised if it was the later:  I've noticed Office apps connected by
>automation can crash the client.

No traceback. the interpreter crashes when Word throws up a dialogue box
saying that it cannot translate a particular doc file to html.


Wed, 24 Mar 2004 03:04:21 GMT  
 problem controlling MS Word from Python

Quote:


> >Can you clarify what you mean by "crashes"?  Does python return a
> >traceback, or does the python interpreter itself crashes?  I wouldn't be
> >surprised if it was the later:  I've noticed Office apps connected by
> >automation can crash the client.

> No traceback. the interpreter crashes when Word throws up a dialogue box
> saying that it cannot translate a particular doc file to html.

Nifty.  It sounds like there's a bug in the OLE code in Python then, or a
dangerous Automation interface in Office. Considering the number of times
Office Automation has hooped my Perl scripts (back off, language-dogs!!
:-) ), I'm inclined to blame Office.

Could you post some sample of your code?  I have Office 97 on my machine
(with HTML saving extensions).  I'd be interested to see if it happens in
the older Office as well.



Wed, 24 Mar 2004 11:18:55 GMT  
 problem controlling MS Word from Python


Quote:

>Could you post some sample of your code?  I have Office 97 on my machine
>(with HTML saving extensions).  I'd be interested to see if it happens
>in the older Office as well.

I did post some earlier, and I'm reulctant to clutter the list, but here is
the relevant bit again: it is office 97 that I have been using. Some lines
have wrapped, but it should be ovious which, and where. I could post the
whole lot if you really want, but that seems rude -- and pointless unless
you have PyQT.

        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 {*filter*}up! ' + 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)



Wed, 24 Mar 2004 17:29:16 GMT  
 problem controlling MS Word from Python

Quote:



>>Could you post some sample of your code?  I have Office 97 on my machine
>>(with HTML saving extensions).  I'd be interested to see if it happens
>>in the older Office as well.

> I did post some earlier, and I'm reulctant to clutter the list, but here is
> the relevant bit again: it is office 97 that I have been using. Some lines
> have wrapped, but it should be ovious which, and where. I could post the
> whole lot if you really want, but that seems rude -- and pointless unless
> you have PyQT.

Is is possible to reduce it down to a small, stand-alone script that we
can run to see the crash?  If you can get it down to 10 lines or so that
I can execute and see it die, then there is much more chance of seeing a
solution...

Mark.

- Show quoted text -

Quote:

>         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 {*filter*}up! ' + 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)



Thu, 25 Mar 2004 10:56:44 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Class(y) 2.4b, with documentation for MS-Word 97, MS-Word 6.0 and WordPerfect 7.0

2. Control of MS Word

3. Control MS Word?

4. Python And MS Word Com Server

5. python COM help with MS word

6. Using Python to work with MS Word.

7. DDE Command reference for MS Office / MS Word

8. MS Word Problems and Clarion OLE

9. Automating Word from WinNT Python Service problem

10. Python w/MS Outlook / MS Exchange

11. MS Access recordset problem w/python

12. Automation control of MS Word; problem inspecting the document

 

 
Powered by phpBB® Forum Software