Back to index

plone3  3.1.7
winword.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
00004 # All Rights Reserved.
00005 # 
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
00008 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00009 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00010 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00011 # FOR A PARTICULAR PURPOSE.
00012 # 
00013 ##############################################################################
00014 """External Editor MSWord Plugin
00015 
00016 $Id: winword.py 67529 2005-03-03 19:22:57Z chrism $
00017 """
00018 
00019 import os
00020 from time import sleep
00021 import win32com
00022 import pythoncom
00023 from win32com import client # Initialize Client module
00024 
00025 class EditorProcess:
00026     def __init__(self, file):
00027         """Launch editor process"""
00028         word = win32com.client.Dispatch('Word.Application')
00029         # Try to open the file, keep retrying until we succeed or timeout
00030         i = 0
00031         timeout = 45
00032         while i < timeout:
00033             try:
00034                 word.Documents.Open(file)
00035             except:
00036                 i += 1
00037                 if i >= timeout:
00038                     raise RuntimeError('Could not launch Word.')
00039                 sleep(1)
00040             else:
00041                 break
00042         word.Visible = 1
00043         self.wordapp = word
00044         self.file = file
00045         
00046     def wait(self, timeout):
00047         """Wait for editor to exit or until timeout"""
00048         sleep(timeout)
00049             
00050     def isAlive(self):
00051         """Returns true if the editor process is still alive"""
00052               try:
00053             self.wordapp.Documents(self.file)
00054         except pythoncom.com_error, why:
00055             # COM will reject the call to access the doc if the user is
00056             # doing anything interactive at the time the call is made.  The
00057             # symptom is a COM error numbered -2147418111 named "Call rejected
00058             # by callee".  This is most critically seen when the user has
00059             # modified the document but chooses "exit" without first saving.
00060             # Without this check in place, the document is not saved back to
00061             # Zope, but no error is raised.
00062             if why[0] == -2147418111:
00063                 return 1
00064         except:
00065             return 0
00066         else:
00067             return 1
00068         
00069 def test():
00070     import os
00071     from time import sleep
00072     from tempfile import mktemp
00073     fn = mktemp('.html')
00074     f = open(fn, 'w')
00075     f.write('<html>\n  <head></head>\n'
00076             '<body><h1>A Test Doc</h1></body>\n</html>')
00077     f.close()
00078     print 'Connecting to Word...'
00079     f = EditorProcess(fn)
00080     print 'Attached to %s %s' % (`f.wordapp`, f.wordapp.Version)
00081     print ('%s is open...' % fn),
00082     if f.isAlive():
00083         print 'yes'
00084         print 'Test Passed.'
00085     else:
00086         print 'no'
00087         print 'Test Failed.'
00088     
00089 if __name__ == '__main__':
00090     test()