Microsoft Office Word and Python (Win XP)

Marco Nawijn nawijn at gmail.com
Sat Jan 9 12:58:48 EST 2010


On Jan 9, 4:12 pm, "3lvss0... at gmail.com" <3lvss0... at gmail.com> wrote:
> Hi.
> Im very new with python. I have got some answer on my issue to use
> interop or COM ''plugins'' to access MS Word through python but i
> don't even know what those two ''plugins'' are so I cannot use them.
> What I want to do is the following:
>
> I need the script that moves (only moves, not change or delete!)
> entire (100% of the text) text from one .doc file to another. But its
> not so easy as it sounds. The target .doc file is not the only one but
> can be many of them. All the target .doc files are always in the same
> folder (same path) but all of them don't have the same name. The .doc
> file FROM where I want to move entire text is only one, always in the
> same folder (same path) and always with the same file name.
> Names of the target are only similar but as I have said before, not
> the same. Here is the point of whole script:
> Target .doc files have the names:
> HD1.doc
> HD2.doc
> HD3.doc
> HD4.doc
> and so on
>
> What I would like to have is moved the entire (but really all of the
> text, must be 100% all) text into the .doc file with the highest ( ! )
> number. The target .doc files will always start with ''HD'' and always
> be similar to above examples.
> It is possible that the doc file (target file) is only one, so only
> HD1.doc. Therefore ''1'' is the maximum number and the text is moved
> into this file.
> Sometimes the target file is empty but usually won't be. If it won't
> be then the text should be moved to the end of the text, into first
> new line (no empty lines inbetween).
> So for example in the target file which has the maximum number in its
> name is the following text:
>
> a
> b
> c
>
> In the file from which I want to move the text is:
>
> d
>
> This means I need in the target file this:
>
> a
> b
> c
> d
>
> Could someone tell me please how to do this?
>
> Thank you.

Hi,

I will try to head you in the right direction with the Python/MS.Word
link.

First of all, you need to install the win32 extension. See
    http://sourceforge.net/projects/pywin32/

Once you have this installed you can instantiate a MS.Word application
like
this (code untested):

>>> from win32com.client import Dispatch
>>> app = Dispatch("Word.Application")
>>> app.Visible = True

The code so-far is more or less equivalent to opening Word without
opening
a document (normally Word will start with an empty document).

To open a document do something like the following.
>>> doc = app.Documents.Open("c:\\example.doc")

Further builtin Python modules that could be helpfull are:
   glob        ->  for searching files matching a pattern
   os, os.path ->  for path related functionality like stripping
directory
                   names from a complete path

Take a look at the online documentation for more information
    http://docs.python.org/modindex.html

Good luck and let us know the result.

Marco



More information about the Python-list mailing list