Open existing Word document, modify and save.

Mike Driscoll kyosohma at gmail.com
Tue Jan 15 13:51:32 EST 2008


On Jan 15, 12:01 pm, gsal <salger... at gmail.com> wrote:
> New to Python and new to Win32. Help please.
>
> O.k., so this might seem like one of those "can you do my homework"
> kind of postings, except that is not homework, it is officework.
>
> I have been reading the Core Python book and I am really excited about
> starting my next project with Python, instead of tcl/tk/Fortran/C.
> While I can see how to use it for scientific computing and have gone
> with the NumPy and SciPy tutorials, I am having a hard time with
> Win32.
>
> Attempted to read some of the documentation for Win32 and while the
> basic stuff talks about creating a document, I did not see how to open
> an existing document and modify certain parts of it...and then it
> seems that it got very complicated (for me) really quick.  Sure, it
> seems powerful, but all I need is a small part of it, I think.
>
> Here is the thing. Here at the office, we have computer programs to
> generate spec data and the Applications people have MS-Word documents
> in a very specific format where they would like to put such spec
> data.
>
> Initially, I was using merge fields and VB macros, until they stopped
> working when newer (non-backwards-compatible) versions came along;
> then, I switched to generating *.xml out of the original *.doc file,
> breaking it appart, modifying it and gluing back together...this is
> very laborious task and I have to go through the whole exercise,
> should the application people modify the original *.doc
>
> So, basically, I am looking for a way to work directly with the
> original *.doc, assuming I know everything about it...which I do; in
> fact, I have it and can also modify it, should I need to bookmark it
> or otherwise modify as needed.
>
> How to go about it?
>
> What's the python code to open an existing document, modify it and
> save it back?
>
> how does the Word document needs to be bookmarked/formatted so that it
> can be modified? For example, after the computer program has been run
> and the new, say, "machine rating" calculated, I need to deposit such
> value in a very specific spot in the Word document.
>
> I presume somebody out there already does this and is willing to
> provide some code?.
>
> Thank you very much in advance.
>
> gsal

As I understand it, the only way to work with MS Word in Python is
through COM in much the same manner as VB does. So if you know the COM
model that MS Word uses, you shouldn't have too much trouble. I would
recommend getting Hammond's and Robinson's book "Python Programming on
Win32" though, as it explains using Python this way. They have a
sample chapter here: http://www.oreilly.com/catalog/pythonwin32/chapter/ch12.html

It should be noted that Hammond is the author of the PyWin32 modules.
ActiveState's website has a special version of Python that includes a
COM browser and you can install it without harming your current
installation. At least, I've had no problems. Other than that, you'll
probably be spending a lot of time on MSDN.

Mike




More information about the Python-list mailing list