我正在使用Python docx库生成一个文档,我希望能够打开并找到拼写错误。但是当我打开文档时,没有任何拼写错误标记(小红色下划线)或者是确定我是否进行了拼写检查。如果我编辑一行或副本,将内容剪切并粘贴到单词中,拼写功能将按预期工作。
是否有某种方法可以让输出文档自动显示/识别输出文档中的拼写错误?我玩过“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')
答案 0 :(得分:4)
我会尝试使用document.settings
对象来包装文档的lxml节点元素。
正如您从文档中看到的那样,hideSpellingErrors
属性。
<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
设置的更多信息: