Solved

Microsoft Word Automation, Zope, Plone, External Editor -- Interesting Problem

Posted on 2006-06-21
5
248 Views
Last Modified: 2010-04-16
Basically -- we are using External Editor (0.9.1) loaded from Plone (2.1rc2 - customized) -- which is of course running on top of Zope (2.8.2), on top of Python (2.3.5).  I am looking to modify the client side component of External Editor to change the default behavior of Microsoft Word (2003, SP2) when used with External Editor.  When you hit "Save", I want it to do the equiv of going to "Save As", selecting "Filtered HTML", selecting the proper temporary file name (.../.../.../something.html), and saying "yes" to the warning that it could "lose data" in the conversion.

The goal is that from an end user perspective, they simply click "External Editor" (which automatically loads word), edit the file, click “save” (or control-s) and close the file – the magic happens on the backend.  

Bonus points if you can find a way to reload the page (if still up) in the web browser to instantly reflect the changes made.  

I will give credit for complete solutions, or even just good brainstorming that gets me on the right path to a solution.  Thanks Much!
0
Comment
Question by:rmelton
  • 3
5 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 17017599
I hope that the code from the self-standing simple script can be used anywhere. Store the followint into the file EditDoc.py or whatever, modify the full path to the winword (should probably get it from the registry). I tried the script to be rather brief and simple. You probably want to add more code to solve the things not related to the principle.

The trick is to create the resulting html file before calling winword. It can be empty, but it should containt he doctype, head etc. This way, you can use the simple Save in the Word after editing (no need to Save As). After finishing the Word, the webbrowser is launched with the resulting file. Then winword is called and after the webbrowser refreshed (in a simple way).

EditDoc.py
---------------------------------------------------------------------------------------------
import os
import webbrowser

docTemplate = '''\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>My document title</title>
</head>
<body>
<h1>Top level title</h1>

<p>This is the doc template. Edit it, save it (do not change the name),
and quit the editor.</p>

</body>
</html>
'''


# Get the file name -- here in current directory, for simplicity.
fname = 'output.html'

# If the file does not exist, save the template under name of the future result.
if not os.path.isfile(fname):
    f = file(fname, 'w')
    f.write(docTemplate)
    f.close()

# Get the full path to the extenal editor -- here hard-coded, for simplicity.
editor = 'D:\Program Files\Microsoft Office\OFFICE11\winword.exe'
editorReadable = 'WinWord'

# Execute the editor and wait until finished.
code = os.spawnl(os.P_WAIT, editor, editorReadable, fname)

# Open the browser with the result.
webbrowser.open(fname)

# Reopen the existing file in the editor and wait.
code = os.spawnl(os.P_WAIT, editor, editorReadable, fname)

# Refresh the browser content and raise its windows.
webbrowser.open(fname, new=False, autoraise=True)
---------------------------------------------------------------------------------------------

 
0
 
LVL 28

Expert Comment

by:pepr
ID: 17017628
Ooops, the backslashes in the winword path should be doubled or changed to normal slashes or the string literal should be written as raw.

editor = r'D:\Program Files\Microsoft Office\OFFICE11\winword.exe'
0
 
LVL 28

Expert Comment

by:pepr
ID: 17192025
Does it work for you?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now