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

Posted on 2006-06-21
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!
Question by:rmelton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 29

Accepted Solution

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 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).
import os
import webbrowser

docTemplate = '''\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <title>My document title</title>
<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>


# 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')

# 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.

# 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., new=False, autoraise=True)

LVL 29

Expert Comment

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'
LVL 29

Expert Comment

ID: 17192025
Does it work for you?

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

734 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