Search & Replace in MS Word Puzzle

Discussion in 'Python' started by Ola K, Dec 9, 2006.

  1. Ola K

    Ola K Guest

    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.ActiveDocument
    find = doc.Content.Find
    w=win32com.client.constants

    #creating the needed styles if not there already
    if "Italic" not in doc.Styles:
    doc.Styles.Add("Italic",w.wdStyleTypeCharacter)
    #"ItalicBi" is the same as "Italic", but for languages that go Right to
    Left.
    doc.Styles("Italic").Font.ItalicBi = True
    print "Italic style was created"
    if "Bold" not in doc.Styles:
    doc.Styles.Add("Bold",w.wdStyleTypeCharacter)
    doc.Styles("Bold").Font.BoldBi = True
    print "Bold style was created"
    if "English Text" not in doc.Styles:
    doc.Styles.Add("English Text", w.wdStyleTypeCharacter)
    doc.Styles("English Text").Font.Scaling = 80
    print "English Text style was created"
    if "English Text Italic" not in doc.Styles:
    doc.Styles.Add("English Text Italic", w.wdStyleTypeCharacter)
    doc.Styles("English Text Italic").BaseStyle = "English Text"
    doc.Styles("English Text").Font.Italic = True
    print "English Text Italic style was created"
    if "English Text Bold" not in doc.Styles:
    doc.Styles.Add("English Text Bold", w.wdStyleTypeCharacter)
    doc.Styles("English Text Bold").BaseStyle = "English Text"
    doc.Styles("English Text").Font.Bold = True
    print "English Text Bold style was created"

    #Search & Replacing Hebrew Italics
    find.ClearFormatting()
    find.Font.Italic = True
    find.Format = True
    find.LanguageID = w.wdHebrew
    find.Replacement.ClearFormatting()
    find.Replacement.Style = doc.Styles("Italic")
    find.Execute(Forward=True, Replace=w.wdReplaceAll, FindText='',
    ReplaceWith='')
    print "Italic style was checked"

    #Search & Replacing Hebrew Bolds
    find.ClearFormatting()
    find.Font.Bold = True
    find.Format = True
    find.LanguageID = w.wdHebrew
    find.Replacement.ClearFormatting()
    find.Replacement.Style = doc.Styles("Bold")
    find.Execute(Forward=True, Replace=w.wdReplaceAll, FindText='',
    ReplaceWith='')
    print "Bold style was checked"

    #Search & Replacing English Regulars
    find.ClearFormatting()
    find.LanguageID = w.wdEnglishUS
    find.Font.Italic = False
    find.Font.Bold = False
    find.Replacement.ClearFormatting()
    find.Replacement.Style = doc.Styles("English Text")
    find.Execute(Forward=True, Replace=w.wdReplaceAll, FindText='',
    ReplaceWith='')
    print "English Text style was checked"

    #Search & Replacing English Italics
    find.ClearFormatting()
    find.LanguageID = w.wdEnglishUS
    find.Font.Italic = True
    find.Replacement.ClearFormatting()
    find.Replacement.Style = doc.Styles("English Text Italic")
    find.Execute(Forward=True, Replace=w.wdReplaceAll, FindText='',
    ReplaceWith='')
    print "English Text Italic style was checked"

    #Search & Replacing English Bolds
    find.ClearFormatting()
    find.LanguageID = w.wdEnglishUS
    find.Font.Bold = True
    find.Replacement.ClearFormatting()
    find.Replacement.Style = doc.Styles("English Text Bold")
    find.Execute(Forward=True, Replace=w.wdReplaceAll, 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 "wdStyleTypeCharacter", or "wdEnglishUS"
    How can I solve this one?

    Advice will be very appreciated.

    --Ola
     
    Ola K, Dec 9, 2006
    #1
    1. Advertising

  2. 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 "wdStyleTypeCharacter", or "wdEnglishUS"
    > How can I solve this one?


    This is described in py2exe wiki.
    http://www.py2exe.org/index.cgi/IncludingTypelibs

    Waldemar
     
    Waldemar Osuch, Dec 10, 2006
    #2
    1. Advertising

  3. Ola K

    Ola K Guest

    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
     
    Ola K, Dec 10, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sebb
    Replies:
    2
    Views:
    341
    Miki Tebeka
    Jan 12, 2004
  2. Brian Blais
    Replies:
    1
    Views:
    406
    Bruno Desthuilliers
    Jun 27, 2006
  3. Greg Ewing
    Replies:
    2
    Views:
    372
    Dieter Maurer
    Jun 29, 2006
  4. Abby Lee
    Replies:
    5
    Views:
    473
    Abby Lee
    Aug 2, 2004
  5. Mark Toth
    Replies:
    3
    Views:
    147
    Mark Toth
    Dec 30, 2007
Loading...

Share This Page