How can I do French and English Spell Checking using MS Word from Python

Pater Maximus emayo at home.com
Sun Oct 24 21:33:17 EDT 2004


First, muchas gracias to Tim and Roger. My conclusions follow:

* Tim's code works great for French and English, two languages that were
part of my initial install of Word 2000. But only after I brought up an
instance of Word as Roger suggested.

However, it did not work for German which was installed at a later date.
When I ran it, you could see the German words appearing in the Word window
and being spell checked. Also, a query of the LanguageID showed it set to
German. But a call to SpellCheck only showed English words as being correct.
In other words it appears that spell checking was using a German dictionary
from the Word side and an English dictionary for my program.

After poking around on the web I was able to coble together the following
code for German that worked but was dog slow and got slower as spell
checking progressed. It took about 20 minutes to check 160,000 words in
English and in French using Tim's method. It took four hours for 160,000
German words with three restarts to get the program to pick up the pace.

<some code here>
msword = win32com.client.gencache.EnsureDispatch ("Word.Application")
msword.Visible=0
wordDoc=msword.Documents.Add()
msword.Selection.NoProofing=False
msword.Selection.LanguageID = win32com.client.constants.wdGerman
s = wordDoc.Sentences(1)
for line in inputFile:
    if count%1000==0:
        MainDict = msword.Languages(msword.Selection.LanguageID).NameLocal
        print 'LanguageID=',msword.Selection.LanguageID,' Main
Dictionary=',MainDict
        print 'count=',count,' correct=',correct,'
CorrectCaps=',correctCaps,' CorrectLower=',correctLower,'
Misspelled=',misspelled
    count+=1
    orig = line.split(',')
    word=orig[1]
    word=word.lower()
    s.Text = word #try lower case first
    colSpellErrors = wordDoc.SpellingErrors;
    if colSpellErrors.Count == 0:
        correctLower+=1
        scrubbedFile.write(orig[0]+','+word+','+orig[2]+','+orig[3])
        continue
    word=word.capitalize()
    s.Text = word #see if capitalizing corrects spelling
    colSpellErrors = wordDoc.SpellingErrors;
    if colSpellErrors.Count == 0:
        correctCaps+=1
        scrubbedFile.write(orig[0]+','+word+','+orig[2]+','+orig[3])
        continue
    misspelled+=1
    misspelledFile.write(line)
<more code here>

I know there must be a better way and would be interested in finding it out.
However, I now have spell checked English, French, and German words lists
and the problem is thankfully behind me.

Thanks again...

You can check out the application that I need French, English, and Greman
for at
http://got2know.net/





More information about the Python-list mailing list