Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Microsoft Office Word and Python (Win XP)

Reply
Thread Tools

Microsoft Office Word and Python (Win XP)

 
 
3lvss0809@gmail.com
Guest
Posts: n/a
 
      01-09-2010
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.
 
Reply With Quote
 
 
 
 
Marco Nawijn
Guest
Posts: n/a
 
      01-09-2010
On Jan 9, 4:12*pm, "(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-09-2010
On 1/9/2010 10:12 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-09-2010
On Sat, 9 Jan 2010 07:12:18 -0800 (PST), "(E-Mail Removed)"
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

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


<snip>
> 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).
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
3lvss0809@gmail.com
Guest
Posts: n/a
 
      01-09-2010
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.
 
Reply With Quote
 
Marco Nawijn
Guest
Posts: n/a
 
      01-09-2010
On Jan 9, 8:18*pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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.

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


 
Reply With Quote
 
3lvss0809@gmail.com
Guest
Posts: n/a
 
      01-10-2010
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.
 
Reply With Quote
 
David Monaghan
Guest
Posts: n/a
 
      01-10-2010
On Sat, 9 Jan 2010 11:18:12 -0800 (PST), "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-10-2010
On Sat, 9 Jan 2010 11:18:12 -0800 (PST), "(E-Mail Removed)"
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:


> 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.
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
3lvss0809@gmail.com
Guest
Posts: n/a
 
      01-10-2010
?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Office Excell 2003 & Microsoft Office Word 2003 Angel Eyes Microsoft Certification 2 06-30-2008 09:28 PM
Microsoft office and Microsoft word??????? bdeditch Computer Support 0 02-17-2007 02:50 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments