How to programmatically spellcheck a word using Microsoft Office dictionary for specified language?

43 Views Asked by At

As the title says, I have a list of words which I want to check programmatically using Microsoft Office dictionaries. The second input is the language code (hard-coded in the example for testing) The code below, written in Python works as intended and produces the exact results I've expected, BUT I'm unable to switch languages. It works only for the default Word language and no matter what I do, it still works only with it.

import os
import win32com.client
#hard-coded parameters
input_file = "test_in.txt"
output_file = "test_out.txt"
language_id = 1031 #1033 en-us/1029 cz/1031 de/1034 es/1036 fr/1040 it/1051 sk

if not os.path.exists(input_file):
    print("Input file does not exist.")
    os._exit(-1)

#instantiating Word application
word_app = win32com.client.Dispatch("Word.Application")

#looking up the dictionary object for the language_id, no error control here
dictionary_object = None
languages = word_app.Languages
for language in languages:
    if language.ID == language_id:
        act_dict_obj = language.ActiveSpellingDictionary
        if act_dict_obj:
             dictionary_object = act_dict_obj
             break
print( "langID: " + str(language_id) )
print( "dict: " + str(dictionary_object) )
dictionary_path = dictionary_object.Path + word_app.PathSeparator + dictionary_object.Name 
print( "dictPath: " + dictionary_path )

#GetSpellingSuggestions works only with any document opened
temp_doc = word_app.Documents.Add()
range = temp_doc.Range( 0, 0 )
range.text = "Bla bla bla"
range.LanguageID = language_id

with open(input_file, 'r', encoding='utf-8') as input_fp:
    words = input_fp.read().split()
        
with open(output_file, 'w', encoding='utf-8') as output_fp:
    for word in words:
        #either bool check (two top lines) or suggestion list (two bottom lines)
        #is_good = word_app.CheckSpelling(word,"","",dict )
        #output_fp.write( word + " " + str(is_good ) + "\n" )
        results = word_app.GetSpellingSuggestions(word,"","",dictionary_object )
        output_fp.write( word + ": " + ", ".join( map( lambda sugg: str(sugg), results ) ) + "\n" )

#clean up
temp_doc.Close(False)
word_app.Quit()

Although the documentation for CheckSpelling and GetSpellingSuggestions says that I can provide dictionary object as the fourth parameter, it does not work for me. No matter if I send there object, path or whatever. I'm sure I have all the dictionaries installed. Also in the beginning, when I include text "Bla bla bla" into temporary document and set it to desired language has no effect.

https://learn.microsoft.com/en-us/office/vba/api/word.application.checkspelling
https://learn.microsoft.com/en-us/office/vba/api/word.application.getspellingsuggestions

I'm not looking for any other library for spell checking, I already have such code. I need specifically to use the ones contained in Office.

I used Python only for convenience and I do not assume it's the source of my problem. Actually, I don't know if I have 'problem' or if Office documentation is wrong. I can't find any example which would use the two named functions with more than one parameter.

I did not implement the "last resort" option, ie. copying the (extremely huge) list of input words to temporary document, having it spellchecked and programmatically extracting the results. #fuj

Even changing styles does not change anything.

temp_doc.Activate()
temp_doc.Styles(-66).LanguageID = language_id
temp_doc.Styles(-1).LanguageID = language_id
temp_doc.Content.Style=-1
temp_doc.Content.text = "This is empty text."
temp_doc.Content.LanguageID = language_id
doclang = temp_doc.Content.LanguageID
print( "detected lang: " + str(doclang ) )
0

There are 0 best solutions below