Microsoft Office Word and Python (Win XP)

3

3lvss0809

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

Marco Nawijn

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):

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

Terry Reedy

I need the script that moves (only moves, not change or delete!)
entire (100% of the text) text from one .doc file to another.

If you want to copy files without modification, use the OS copy command.
You can use the subprocess module to do that from Python.
 
D

Dennis Lee Bieber

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:
For Word documents, you WILL have to use such (on Windows the win32
extension library gives you the needed interfaces -- though I've not
used them myself [figuring out how to do what you want using the VBA
built into Word is difficult enough, and that IS pretty much the COM
interface]). Possibly the ctypes module will also give you said
access...
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:

said:
Could someone tell me please how to do this?
How familiar with Python are you?

I'd prototype this using plain text files first, avoiding any usage
of COM/Word access -- just plain file and directory operations.

After you have verified all conditions (destination file exists,
moving to end of contents, reading source file, writing data to
destination -- note that whether the destination contains data or not is
not relevant, you are appending the source text to the /end/ of whatever
is in the destination) are functioning properly THEN study the Word COM
model and replace the file open/close/read/write operations with Word
COM operations to do the same thing. The directory operations to find
the destination file are not Word or COM related and won't change.

The Word COM model is not something you will find documents for in
Python (not even the win32 module docs cover specifics of individual
applications).
 
3

3lvss0809

Marco Nawijn: I have had installed pywin32 already. The three lines
that you mentoined don't do this, also what did you mean with "doc =
app.Documents.Open("c:\\example.doc")". Which document should I open
with this line? It shouldn't be opened anything. I was asking about
the script as automated process. If you know how could i do this?

Terry Reedy: I have never mentoined copying files but moving the whole
text from, always the same (same name, same path), .doc file with.
However copying (=moving) text to correct .doc file would be good yes.
I know command prompt and its copy function but this way it wouldn't
work because I would have to define the target file - the file INTO
which I want to move the text. But I will never know the file name
(target file). The only thing I know is:
- the file is one of .doc files that start with "HD"
- whole name of those .doc file is always HDX.doc where X is a number
and I need to move the text into the file with maximum X (the most
high number)
- all the HD files will be always in the same path (same folder) but I
would like to use the path inside the code (it might be obvious that I
have to) because on PC and laptop, I have two different usernames and
since HD files are located inside Documents And Settings, I have to
use two copies of the script - one for PC, one for laptop.

Dennis Lee Bieber: Im not familiar with python, also Im not
programmer. Thats why Im not able to do so when people tell me "do
this then use XYZ function which will give you ZYX from what you can
do that and you will get result". Im still willing to learn but there
are thousands of python tutorials and the one for exsactly this topic
probably doesn't exsist. The .doc extension is required, so I cannot
use .txt because I need the HD files in .doc.
 
M

Marco Nawijn

Marco Nawijn: I have had installed pywin32 already. The three lines
that you mentoined don't do this
I checked at my own computer and it works fine.
also what did you mean with "doc =
app.Documents.Open("c:\\example.doc")". Which document should I open
with this line?
This was just meant as an example on how to open a Word document from
within python. This would be the basis for copying/appending from your
source
document to your target document (e.g. HD10.doc).
It shouldn't be opened anything. I was asking about
the script as automated process. If you know how could i do this?
Well it is a python script. So you can run it as an automated process.
You should just set app.Visible=False so the Word user interface
component
is not shown.
 
3

3lvss0809

Marco did you also make the HD files to it worked for you? Because I
cannot even imagine how only three lines would do everything - find
correct folder (path) of the files, find maximum number, move the
entire text,... In this 3 lines is not stated that we are talking
about the files that start with "HD" so I wonder how it worked for
you. Nothing was moved on my PC. I have done some research about the
line "app = Dispatch("Word.Application")" and saw this:

http://www.programmingforums.org/post105986.html

The script described here is different comparing to what I want to do
but I might be able to use some ideas - of course, since Im not a
programmer, I need to study the functions before. On the link the user
is trying to replace parts (predefinited or not - i don't know) of
the .doc files - this is not moving/copying entire text but the topic
is still about writing something into .doc through python.
Unfortunatelly for me, his script doesn't need to search for
correct .doc file.

So I kept searching and came to this:

http://www.daniweb.com/forums/thread129924.html

This task is very close to what I want but still different. If we try
to compare;

"I am a text" (his task) = already exsisting (if any) text in my HDX
file where X is the highest number (my task)
"Hello" (his task) = whole text inside .doc with always the same name
on the same location (my task)

then thats it. I believe just those two differences exsist.
 
D

David Monaghan

Dennis Lee Bieber: Im not familiar with python, also Im not
programmer.

What you want to do isn't complicated, but it isn't simple either, unless
you're familiar with VBA/VBS. I approach these problems by first getting the
VBA code by recording a macro within Word. I then convert it to VB Script,
which is a learning process in itself. Converting that script to Python com
is another learning process and then putting the whole thing together with
file finding and saving is another job.

When you've done that, you won't feel able to say you're not a programmer -
and you should feel familiar with Python, too.

DaveM
 
D

Dennis Lee Bieber

Dennis Lee Bieber: Im not familiar with python, also Im not
programmer. Thats why Im not able to do so when people tell me "do
this then use XYZ function which will give you ZYX from what you can
do that and you will get result". Im still willing to learn but there
are thousands of python tutorials and the one for exsactly this topic
probably doesn't exsist. The .doc extension is required, so I cannot
use .txt because I need the HD files in .doc.

And what I suggested as a prototype using plain text files was to
get you to where the Python side of things is working... Then you study
the COM aspect and plug it into the Python (replacing the simple text
I/O operations with COM operations).

This way you don't have to fight with learning both Python and COM
as one unit.
 
A

Alf P. Steinbach

* (e-mail address removed):
21 days has passed and still noone is willing to help :-(

Did you see the reply from Marco Nawin?

If you don't see that reply, dated (a bit less than) 2 hours after your original
posting on the 9th, I can repost it here.

If you have any follow-up questions just post them.



Cheers & hth.,

- Alf
 
S

Steve Holden

21 days has passed and still noone is willing to help :-(

Y'know, the Internet isn't a magic lantern. Perhaps you need to ask your
question differently, or provide more information. Maybe there's
something *you* could do to help you get closer to the answer to *your*
question.

Read the question at the time, don't remember having much to contribute
right then. But you lucked out today: it's Saturday morning and I am at
home in my family room, and it's snowing outside. Hey, at least you do
know already that people will respond to your questions.

Look for a (second-hand?) copy of Andy Robinson's and Mark Hammond's
"Python Programming on Win32", which taught me a lot about COM I have
thankfully since been able to forget. Go to Microsoft channels for
details of the Office object models, and good luck with that (the
quality and quantity of that used to be something of a movable feast,
though I imagine things have improved since).

You might also ask yourself whether IronPython and the .NET interfaces
aren't a better and more modern way of controlling Microsoft products.
The .NET documentation is pretty well organized and complete, I believe.

regards
Steve
 
D

Dave Angel

21 days has passed and still noone is willing to help :-(
ch /willing/able/

I wouldn't say no-one, even then, since there were at least 10 messages
in the thread on the 19th and 20th. Presumably they weren't all from you.

If you were doing this to text files, I would have been happy to help.
But interacting with MS Word isn't easy, and I don't know how. If
you're a beginner as you stated, you need to pick an easier first task.

If you're determined to do it, best advice I could give is to head over
to the python-Win32 mailing list. At least they're used to dealing with
COM, and other MS-specific stuff.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top