473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problems with Python for Windows extensions

KK
the code below is taken from M$ technet as an example on using vb
script to do a replace all in word:

Const wdReplaceAll = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc =
objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
Set objSelection = objWord.Selection

objSelection.Find.Text = "Contoso"
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True

objSelection.Find.Replacement.Text = "Fabrikam"
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll


I did a rewrite and made it pythonic:

from win32com.client import *

wdReplaceAll = 2

objWord = Dispatch("Word.Application")
objWord.Visible = True

objDoc =
objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
objSelection = objWord.Selection

objSelection.Find.Text = "Contoso"
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True

objSelection.Find.Replacement.Text = "Fabrikam"
objSelection.Find.Execute (Replace = wdReplaceAll)
However, the document juz loaded up in word but no action was taken. I
am using Word 2003. Any ideas?

Sep 3 '05 #1
5 1625
"KK" <Ra*****@gmail.com> schrieb im Newsbeitrag
news:11*********************@z14g2000cwz.googlegro ups.com...
| the code below is taken from M$ technet as an example on using vb
| script to do a replace all in word:
|
| Const wdReplaceAll = 2
|
| Set objWord = CreateObject("Word.Application")
| objWord.Visible = True
|
| Set objDoc =
| objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
| Set objSelection = objWord.Selection
|
| objSelection.Find.Text = "Contoso"
| objSelection.Find.Forward = True
| objSelection.Find.MatchWholeWord = True
|
| objSelection.Find.Replacement.Text = "Fabrikam"
| objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
|
|
|
|
| I did a rewrite and made it pythonic:
|
| from win32com.client import *
|
| wdReplaceAll = 2
|
| objWord = Dispatch("Word.Application")
| objWord.Visible = True
|
| objDoc =
| objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
| objSelection = objWord.Selection
|
| objSelection.Find.Text = "Contoso"
| objSelection.Find.Forward = True
| objSelection.Find.MatchWholeWord = True
|
| objSelection.Find.Replacement.Text = "Fabrikam"
| objSelection.Find.Execute (Replace = wdReplaceAll)
|
|
| However, the document juz loaded up in word but no action was taken. I
| am using Word 2003. Any ideas?

KK,

Your example seemed to work fine for me (Python2.4, Pythonwin build 204,
Word 2003)

One thing: since you say your document loads up fine I don't know if it is
just a typo, but make sure you follow the rules of backslash literals in
path names. In other words:
"K:\Development\Fabricbase\prod\Test.doc" should read either

r"K:\Development\Fabricbase\prod\Test.doc" (note the leading r for raw
string
or

"K:\\Development\\Fabricbase\\prod\\Test.doc"
or

"K:/Development/Fabricbase/prod/Test.doc"

--

Vincent Wehren

Sep 4 '05 #2
KK
thx for ur reply
u r rite that i should use a raw string, but that doesn't solve the
problem
i am q annoyed by this strange behaviour.
i tried to run the script on my friend's pc, which is python2.4 + pywin
204 + office 2000, but same thing happened

now i am thinking to generate a vbs from python and run it. i know it
is dumb but i dont know other solutions...

Sep 4 '05 #3
Hi,

Invoking Execute as shown below works.. I have no explanation why your
VB to Py converted code did not work.

wdFindContinue = 1
objSelection.Find.Execute('Contosa', False, True, False, False, True,
True, wdFindContinue, True, 'Fabrikam', wdReplaceAll, False, False,
False, False)

This finds 'Contosa' and replaces all occurances with 'Fabricam'.

For a full explanation about the arguments to Execute, look it up in the
VBA Help.

Thanks,
-Kartic
The Great 'KK' uttered these words on 9/3/2005 11:14 AM:
the code below is taken from M$ technet as an example on using vb
script to do a replace all in word:

Const wdReplaceAll = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc =
objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
Set objSelection = objWord.Selection

objSelection.Find.Text = "Contoso"
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True

objSelection.Find.Replacement.Text = "Fabrikam"
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll


I did a rewrite and made it pythonic:

from win32com.client import *

wdReplaceAll = 2

objWord = Dispatch("Word.Application")
objWord.Visible = True

objDoc =
objWord.Documents.Open("K:\Development\Fabricbase\ prod\Test.doc")
objSelection = objWord.Selection

objSelection.Find.Text = "Contoso"
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True

objSelection.Find.Replacement.Text = "Fabrikam"
objSelection.Find.Execute (Replace = wdReplaceAll)
However, the document juz loaded up in word but no action was taken. I
am using Word 2003. Any ideas?

Sep 4 '05 #4
KK
Hello,
I guess you could reproduce my problem, Kartic. I have tried the one u
suggested, but sadly it didn't work for me. I think the COM of pywin is
quite tricky, or it might be a bug. I have some friends who also had
experience of weird behaviors of pywin32, which makes me skeptical of
using it in real app.

Thanks
KK

Sep 7 '05 #5
The Great 'KK' uttered these words on 9/7/2005 7:57 AM:
Hello,
I guess you could reproduce my problem, Kartic. I have tried the one u
suggested, but sadly it didn't work for me. I think the COM of pywin is
quite tricky, or it might be a bug. I have some friends who also had
experience of weird behaviors of pywin32, which makes me skeptical of
using it in real app.

Thanks
KK


Actually, I have created some robust win32com applications (using Word
and Excel) that work consistently on all installed machines.

Could it be an Office 2003 quirk? Did some other app of yours crash
while using COM? Such crashes could produce unpredictable results in COM
related code, from my experience.

So, if you think it is worth your time, you could probably investigate
it a bit more or better contact Mark Hammond to see if he can help. I
don't know if there is a win32com mailing list; if there is one, please
post your question there too.

Thanks,
-Kartic
Sep 7 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: hokieghal99 | last post by:
While trying to learn more about Python's standard modules, I noticed that there are *a lot* of Mac only modules... more so than Unix and Windows specific modules combined. Is there a reason for...
49
IDE
by: Thomas Lindgaard | last post by:
Hello I am probably going to start a war now... but so be it :) I just want to hear what all you guys who eat pythons for breakfast use for python coding. Currently I use Kate, but I would...
5
by: Fuzzyman | last post by:
Python 2.4 is built with Microsoft Visiual C++ 7. This means that it uses msvcr7.dll, which *isn't* a standard part of the windows operating system. This means that if you build a windows installer...
0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
3
by: Andreas | last post by:
Hi! I'm currently developing a DLL that makes use of C++ and .net (mixed) using Visual Studio 2003. Now, as I wanted to move to the new Visual Studio 2005, I converted this project into the...
1
by: TPJ | last post by:
Hello, I have another, probably stupid, question. I'm working on some Python project, and I use some extensions written in C. I do all the development on my GNU/Linux box, so my setup.py script...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
6
by: JW | last post by:
I have a lousy little Python extension, generated with the generous help of Pyrex. In Linux, things are simple. I compile the extension, link it against some C stuff, and *poof*! everything...
0
by: Kirk Bailey | last post by:
RE: TinyWeb running python in windows PROBLEM: getting python scripts to execute. SOLUTION: Insure the script ends in the name extension .py. Windows associates all such files with the...
15
by: kyosohma | last post by:
Hi, I am trying to get a small group of volunteers together to create Windows binaries for any Python extension developer that needs them, much like the package/extension builders who volunteer...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.