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