Solved

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

Posted on 2006-06-21
5
244 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 …

746 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

13 Experts available now in Live!

Get 1:1 Help Now