使用docx模块以Python3.6检查.docx格式的复选框

时间:2019-03-31 08:24:13

标签: ms-word python-3.6 python-docx

使用下面的代码使用python-docx插入空复选框,现在需要插入标记为使用python-docx选中的复选框

run = para.add_run()
tag = run._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)
tag.append(start)
run2 = para.add_run()    

tag2 = run2._r
fld = docx.oxml.shared.OxmlElement('w:fldChar')
fld.set(docx.oxml.ns.qn('w:fldCharType'), 'begin')
fldData = docx.oxml.shared.OxmlElement('w:fldData')

fldData.text='/////2UAAAAUAAYAQwBoAGUAYwBrADEAAAAAAAAAAAAAAAAAAAAAA'
fldData.set(docx.oxml.ns.qn('xml:space'), 'preserve')
fld.append(fldData)
tag2.append(fld)    

checker = docx.oxml.shared.OxmlElement('w:checkBox')
sizer = docx.oxml.shared.OxmlElement('w:sizeAuto')
checkValue = docx.oxml.shared.OxmlElement('w:default')
checkValue.set(docx.oxml.ns.qn('w:val'), "true")
checker.append(sizer)
checker.append(checkValue)
start.append(checker)    
tag.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)

现在需要插入使用python-docx标记为已选中的复选框

0 个答案:

没有答案