473,605 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search & Replace in MS Word Puzzle

Hi guys,
I wrote a script that works *almost* perfectly, and this lack of
perfection simply puzzles me.
I simply cannot point the whys, so any help on it will be appreciated.
I paste it all here, the string at the beginning explains what it does:

'''A script for MS Word which does the following:
1) Assigns all Hebrew italic characters "Italic" character style.
2) Assigns all Hebrew bold characters "Bold" character style.
2) Assign all English US characters "English Text" (can be regular,
Italic, or Bold)

'''
import win32com.client

#setting up shortcuts
word = win32com.client .Dispatch("Word .Application")
doc = word.ActiveDocu ment
find = doc.Content.Fin d
w=win32com.clie nt.constants

#creating the needed styles if not there already
if "Italic" not in doc.Styles:
doc.Styles.Add( "Italic",w.wdSt yleTypeCharacte r)
#"ItalicBi" is the same as "Italic", but for languages that go Right to
Left.
doc.Styles("Ita lic").Font.Ital icBi = True
print "Italic style was created"
if "Bold" not in doc.Styles:
doc.Styles.Add( "Bold",w.wdStyl eTypeCharacter)
doc.Styles("Bol d").Font.Bol dBi = True
print "Bold style was created"
if "English Text" not in doc.Styles:
doc.Styles.Add( "English Text", w.wdStyleTypeCh aracter)
doc.Styles("Eng lish Text").Font.Sca ling = 80
print "English Text style was created"
if "English Text Italic" not in doc.Styles:
doc.Styles.Add( "English Text Italic", w.wdStyleTypeCh aracter)
doc.Styles("Eng lish Text Italic").BaseSt yle = "English Text"
doc.Styles("Eng lish Text").Font.Ita lic = True
print "English Text Italic style was created"
if "English Text Bold" not in doc.Styles:
doc.Styles.Add( "English Text Bold", w.wdStyleTypeCh aracter)
doc.Styles("Eng lish Text Bold").BaseStyl e = "English Text"
doc.Styles("Eng lish Text").Font.Bol d = True
print "English Text Bold style was created"

#Search & Replacing Hebrew Italics
find.ClearForma tting()
find.Font.Itali c = True
find.Format = True
find.LanguageID = w.wdHebrew
find.Replacemen t.ClearFormatti ng()
find.Replacemen t.Style = doc.Styles("Ita lic")
find.Execute(Fo rward=True, Replace=w.wdRep laceAll, FindText='',
ReplaceWith='')
print "Italic style was checked"

#Search & Replacing Hebrew Bolds
find.ClearForma tting()
find.Font.Bold = True
find.Format = True
find.LanguageID = w.wdHebrew
find.Replacemen t.ClearFormatti ng()
find.Replacemen t.Style = doc.Styles("Bol d")
find.Execute(Fo rward=True, Replace=w.wdRep laceAll, FindText='',
ReplaceWith='')
print "Bold style was checked"

#Search & Replacing English Regulars
find.ClearForma tting()
find.LanguageID = w.wdEnglishUS
find.Font.Itali c = False
find.Font.Bold = False
find.Replacemen t.ClearFormatti ng()
find.Replacemen t.Style = doc.Styles("Eng lish Text")
find.Execute(Fo rward=True, Replace=w.wdRep laceAll, FindText='',
ReplaceWith='')
print "English Text style was checked"

#Search & Replacing English Italics
find.ClearForma tting()
find.LanguageID = w.wdEnglishUS
find.Font.Itali c = True
find.Replacemen t.ClearFormatti ng()
find.Replacemen t.Style = doc.Styles("Eng lish Text Italic")
find.Execute(Fo rward=True, Replace=w.wdRep laceAll, FindText='',
ReplaceWith='')
print "English Text Italic style was checked"

#Search & Replacing English Bolds
find.ClearForma tting()
find.LanguageID = w.wdEnglishUS
find.Font.Bold = True
find.Replacemen t.ClearFormatti ng()
find.Replacemen t.Style = doc.Styles("Eng lish Text Bold")
find.Execute(Fo rward=True, Replace=w.wdRep laceAll, FindText='',
ReplaceWith='')
print "English Text Bold style was checked"

print "We are done."
word.Visible=1

----------Code end here

So generally speaking this script works quite nicely, BUT:

1. Despite this sort of conditions:
" if "Italic" not in doc.Styles: "
if this style already exists I get an error, and the program stops.
Any idea how can that be?...

2. The replacement of the English characters doesn't seem to work very
well. It either assigns all English characters "English Text Bold", or
"English Text Italic" and with no apparent reason.
?....

3. The command
" word.Visible=1 "
doesn't work anymore. I say "anymore" because it used to work, but
later I ran "COM Makepy Utility" on "Microsoft Word 10 Object Library
(8.2)" and since then it stopped working. On Excel, for example, I
never ran Makepy and this commands works fine for it.
Any idea on this one?...

4. In the end of this long weekend I was pretty satisfied with my
script (even if not fully functioning) and used PY2EXE to make it an
..exe file so that I can use it in my work place. But alas, that .exe
file does not work because it doesn't recognize any of win32com client
constants. Such as "wdStyleTypeCha racter", or "wdEnglishU S"
How can I solve this one?

Advice will be very appreciated.

--Ola

Dec 9 '06 #1
2 5077
I do not have answers for all your questions but a few remarks that may
help.

Ola K wrote:
Hi guys,
I wrote a script that works *almost* perfectly, and this lack of
perfection simply puzzles me.
I simply cannot point the whys, so any help on it will be appreciated.
I paste it all here, the string at the beginning explains what it does:

'''A script for MS Word which does the following:
1) Assigns all Hebrew italic characters "Italic" character style.
2) Assigns all Hebrew bold characters "Bold" character style.
2) Assign all English US characters "English Text" (can be regular,
Italic, or Bold)
-- Code snipped
So generally speaking this script works quite nicely, BUT:

1. Despite this sort of conditions:
" if "Italic" not in doc.Styles: "
if this style already exists I get an error, and the program stops.
Any idea how can that be?...
doc.Styles is a container (a build in Word object) holding instances of
Styles
(another build in Word object). One way to make the intended check
would be.
style_names = set(s.NameLocal for s in doc.Styles)
if "Italic" not in style_names:
# create style
>
2. The replacement of the English characters doesn't seem to work very
well. It either assigns all English characters "English Text Bold", or
"English Text Italic" and with no apparent reason.
?....
Read about Range object in Word VBA documentation. Range.Collapse may
explain what happens here.
>
3. The command
" word.Visible=1 "
doesn't work anymore. I say "anymore" because it used to work, but
later I ran "COM Makepy Utility" on "Microsoft Word 10 Object Library
(8.2)" and since then it stopped working. On Excel, for example, I
never ran Makepy and this commands works fine for it.
Any idea on this one?...
This should work. If word.visible = True used to work but stopped
after running Makepy it could be explained. Looks to me there are some
other factors in play.
>
4. In the end of this long weekend I was pretty satisfied with my
script (even if not fully functioning) and used PY2EXE to make it an
.exe file so that I can use it in my work place. But alas, that .exe
file does not work because it doesn't recognize any of win32com client
constants. Such as "wdStyleTypeCha racter", or "wdEnglishU S"
How can I solve this one?
This is described in py2exe wiki.
http://www.py2exe.org/index.cgi/IncludingTypelibs

Waldemar

Dec 10 '06 #2
Waldemar Osuch wrote:
1. doc.Styles is a container (a build in Word object) holding instances of
Styles (another build in Word object). One way to make the intended check
would be.
style_names = set(s.NameLocal for s in doc.Styles)
if "Italic" not in style_names:
# create style
I changed the code to that and it works perfectly! How come this is
better? I mean, I didn't find any logical or syntax problem with the
way it was before.
2. Read about Range object in Word VBA documentation. Range.Collapse may
explain what happens here.
Well, so I did, and I can see now that the problem is probably that
Word takes into consideration the previous characters as well, unless I
collapse the range. However, I don't seem to find the way to properly
do it, syntax-wise. I only managed to collapse the Selection, which
didn't do the trick.
>4. This is described in py2exe wiki.
http://www.py2exe.org/index.cgi/IncludingTypelibs
Thanks. I am now implementing this.

--Ola

Dec 10 '06 #3

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

Similar topics

5
2344
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
1
8713
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast, returned the hyperlinked page title, filename, and the body txt (30 preceding and following words) in context with the search word highlighted. Excellent.! See it working at: http://www.ipt.co.za Just search for "firearm"
14
4620
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like eBay, Yahoo and others. This is what she wants my program to be able to do: (try this test at different websites just for fun). At eBay: - enter the word 'television' in a search field à you will get 2155 items.
1
1956
by: Tomomichi Amano | last post by:
Could some one tell me how I can seach and replace only one word in a textBox (THE FIRST WORD THAT COMES AFTER THE CURSOR). I already know how to replace ALL , but I don't know how to REPLACE one, and how to SEARCH one and select that point. Thank in advance
1
1665
by: Tomomichi Amano | last post by:
Hello. I want to make replace & search functions in my text editor. Thanks to the kind people here at the newsgroup, I was able to make the function. But I was not able to understand how to REPLACE the next word (the nearest word from the cursor; not REPLACE ALL, but replace only one word) and SEARCH the next word. COuld some one help me? Thanks in advance.
3
8250
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}") Thanks, Craig
1
13082
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
2009
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
2
2692
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio -- AND NOW FOR A WORD (an IF blog):
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8288
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.