使用 Python docx 时如何在输出文档中启用拼写?

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

我正在使用 Python docx 库生成一个文档,我希望能够打开该文档并在其中查找拼写错误。但是当我打开 none 的文档时拼写错误标志(红色小下划线)或在我 运行 拼写检查时被识别。如果我编辑一行或复制、剪切内容并将其粘贴回 word,拼写功能将按预期工作。

有什么方法可以让输出文档自动解决 display/recognize 输出文档中的拼写错误?我玩过 "no_proof" 标志,但这似乎没有帮助。 (在 Windows 框上使用 64 位 Python 3.4,docx 8.6,在 Word 2013 中打开输出)

感谢任何想法!

重现代码:

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

Word 输出:

我会尝试使用 document.settings 对象来包装文档的 lxml 节点元素。 正如您从文档中看到的那样,有 hideSpellingErrors 属性。

Python DOCX Settings

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

编辑: 在进一步研究之后,我会尝试这样的事情:

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")

带有可以更改的 DOCX 前缀,但在 lxml 元素中很容易读取。 我现在不知道是否有办法更直接、更干净地做事,但这对我有用。

有关 docx 中 proofState 设置的更多信息: