11

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 :

ROAR
  • 908
  • 1
  • 8
  • 25
Colin Talbert
  • 424
  • 6
  • 19

1 Answers1

6

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:

GendoIkari
  • 224
  • 1
  • 7
  • Worked like a charm! Thank you. – Colin Talbert Aug 31 '17 at 19:58
  • 2
    Use nsmap to help futureproof the code: `nsmap = document.settings.element.nsmap; proofstate = document.settings.element.find("w:proofState", nsmap); proofstate.set("{{{}}}grammar".format(nsmap["w"]), "dirty"); proofstate.set("{{{}}}spelling".format(nsmap["w"]), "dirty")` – Sam Bull Feb 16 '19 at 12:30