Solved

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

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

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 29

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 29

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
os.getenviron does not work when running script from crontab 10 56
Copy Files - Python 7 88
yara deployment 8 78
phython 2 19
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 …

831 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