Search code examples
pythondocxpython-docx

When using Python docx how to enable spelling in output document?


I'm using the Python docx library to generate an document, that I would like to be able to open and find spelling errors in. But when I open the document none of the spelling errors flag (little red underlines) or are identified if I run a spell check. If I edit a line or copy, cut and paste the content back into word the spelling functionality works as expected.

Is there some way to get the output document to display/recognize spelling errors in the output document automatically? I've played around with the "no_proof" flag but that doesn't seem to help. (Using 64 bit Python 3.4, docx 8.6 on a Windows box, Opening output in Word 2013)

Thanks for any ideas!

Code to reproduce:

from docx import Document
document = Document()
paragraph = document.add_paragraph('This has a spellling errror')
document.save(r'SpellingTester.docx')

Output in Word :


Solution

  • I would try using document.settings object that it's wrapping the lxml node element of the document. As you can see from the documentation there's the hideSpellingErrors attribute.

    Python DOCX Settings

    <xsd:element name="hideSpellingErrors" type="CT_OnOff" minOccurs="0"/>
    

    EDIT: After researching a little further I would try something like that:

    import docx
    
    document = docx.Document()
    
    DOCX = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
    
    element = document.settings.element.find(DOCX + 'proofState')
    element.attrib[DOCX + 'grammar'] = 'dirty'
    element.attrib[DOCX + 'spelling'] = 'dirty'
    
    document.add_paragraph("This has a spellling errror")
    document.save("test.docx")
    

    With DOCX prefix that can change, but it's easily read in the lxml element. I don't know right now if there's a way to do things more directly and cleanly but this works for me.

    More about proofState setting in docx: