python-3.x - python-docx : adding checkbox to . docx 不起作用

标签 python-3.x docx


我想使用 python-docx 库将 xml 添加到我的 .docx 文档中。我从 stackoverflow 尝试了这段代码,但它不起作用,我不知道为什么。使用 LibreOffice 和 Microsoft Word 打开 docx 时什么也没有得到。

table = document.add_table(rows=1, cols=1)
p = table.cell(0, 0).paragraphs[0]
run = p.add_run()
tag = run._r
start = docx.oxml.shared.OxmlElement('w:bookmarkStart')
start.set(docx.oxml.ns.qn('w:id'), '0')
start.set(docx.oxml.ns.qn('w:name'), '0')
tag.append(start)

ctype = docx.oxml.OxmlElement('w:complexType')
ctype.set(docx.oxml.ns.qn('w:name'), 'CT_FFCheckBox')
seq = docx.oxml.OxmlElement('w:sequence')
choice = docx.oxml.OxmlElement('w:choice')
el = docx.oxml.OxmlElement('w:element')
el.set(docx.oxml.ns.qn('w:name'), 'size')
el.set(docx.oxml.ns.qn('w:type'), 'CT_HpsMeasure')
el2 = docx.oxml.OxmlElement('w:element')
el2.set(docx.oxml.ns.qn('w:name'), 'sizeAuto')
el2.set(docx.oxml.ns.qn('w:type'), 'CT_OnOff')

choice.append(el)
choice.append(el2)

el3 = docx.oxml.OxmlElement('w:element')
el3.set(docx.oxml.ns.qn('w:name'), 'default')
el3.set(docx.oxml.ns.qn('w:type'), 'CT_OnOff')
el3.set(docx.oxml.ns.qn('w:minOccurs'), '0')
el4 = docx.oxml.OxmlElement('w:element')
el4.set(docx.oxml.ns.qn('w:name'), 'checked')
el4.set(docx.oxml.ns.qn('w:type'), 'CT_OnOff')
el4.set(docx.oxml.ns.qn('w:minOccurs'), '0')

seq.append(choice)
seq.append(el3)
seq.append(el4)

ctype.append(seq)
start.append(ctype)

end = docx.oxml.shared.OxmlElement('w:bookmarkEnd')
end.set(docx.oxml.ns.qn('w:id'), '0')
end.set(docx.oxml.ns.qn('w:name'), '0')
tag.append(end)

你能帮我吗?非常感谢
PS:我找到代码here

最佳答案

调整 this SO post 中的函数,我让它工作了。

import docx
import random

def addCheckbox(para, box_id, name):
    run = para.add_run()
    tag = run._r
    fld = docx.oxml.shared.OxmlElement('w:fldChar')
    fld.set(docx.oxml.ns.qn('w:fldCharType'), 'begin')

    ffData = docx.oxml.shared.OxmlElement('w:ffData')
    e = docx.oxml.shared.OxmlElement('w:name')
    e.set(docx.oxml.ns.qn('w:val'), 'Check1')
    ffData.append(e)
    ffData.append(docx.oxml.shared.OxmlElement('w:enabled'))
    e = docx.oxml.shared.OxmlElement('w:calcOnExit')
    e.set(docx.oxml.ns.qn('w:val'), '0')
    ffData.append(e)
    e = docx.oxml.shared.OxmlElement('w:checkBox')
    e.append(docx.oxml.shared.OxmlElement('w:sizeAuto'))
    ee = docx.oxml.shared.OxmlElement('w:default')
    ee.set(docx.oxml.ns.qn('w:val'), '0')
    e.append(ee)
    ffData.append(e)

    fld.append(ffData)
    tag.append(fld)

    run2 = para.add_run()
    tag2 = run2._r
    start = docx.oxml.shared.OxmlElement('w:bookmarkStart')
    start.set(docx.oxml.ns.qn('w:id'), str(box_id))
    start.set(docx.oxml.ns.qn('w:name'), name)
    tag2.append(start)

    run3 = para.add_run()
    tag3 = run3._r
    instr = docx.oxml.OxmlElement('w:instrText')
    instr.text = 'FORMCHECKBOX'
    tag3.append(instr)

    run4 = para.add_run()
    tag4 = run4._r
    fld2 = docx.oxml.shared.OxmlElement('w:fldChar')
    fld2.set(docx.oxml.ns.qn('w:fldCharType'), 'end')
    tag4.append(fld2)

    run5 = para.add_run()
    tag5 = run5._r
    end = docx.oxml.shared.OxmlElement('w:bookmarkEnd')
    end.set(docx.oxml.ns.qn('w:id'), str(box_id))
    end.set(docx.oxml.ns.qn('w:name'), name)
    tag5.append(end)

if __name__ == '__main__':
    document = docx.Document()
    document.add_heading('Document Title', 0)
    p1 = document.add_paragraph('A paragraph with a checkbox ')
    addCheckbox(p1, random.randint(16*1024, 32*1024), 'justinwashere')
    document.save('demo.docx')

我在函数中更改了他的 ffData 内容,以匹配他原始问题中的替换 XML。

关于python-3.x - python-docx : adding checkbox to . docx 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551746/

相关文章:

python - 在子图中绘制网格 Python matplotlib

python - 有没有办法读取.docx文件包括使用python-docx的自动编号

pdf - Word Automation Service 断开目录中的链接

python - TypeError 对象没有参数

python - 查找最后一个可能的索引值以满足过滤要求

python - python中的子进程不产生输出

python - 如何查看日期在哪个日期范围内并相应地分配值?

file - 验证 doc docx 类型文件时出现 Laravel 5.1 错误

php - 使用 PHP 为目录中的文件显示 docx 属性(标题、标签)

python - 如何在 mac osx 上安装 Python docx 模块