Linked Questions

Popular Questions

Get checkbox values in docx file using python

Asked by At

I have a check box in word docx file. I am able to identify when a checkbox is present but I am not able to retrieve the w:default value (See below where the default value is 1 which means the check box is set)

<w:ffData>
    <w:name w:val="MyCheck"/>
    <w:enabled/>
    <w:calcOnExit w:val="0"/>
    <w:checkBox>
        <w:sizeAuto/>
        <w:default w:val="1"/>
    </w:checkBox>
</w:ffData>

Using the following code I only get "None" returned for the checkbox value (w:val) and default value (w:default)- any ideas how I can access these values for each checkbox?

from docx import Document
from docx.oxml.ns import qn

document = Document('C:\MyTest\\onecheckbox.docx')

doc_elm = document._element
checkBoxes = doc_elm.xpath('.//w:checkBox')
for checkBox in checkBoxes:
    print('checkBox value is %s' % checkBox.get(qn('w:val')))
    print('default value is %s' % checkBox.get(qn('w:default')))

Thanks

J

Unable to determine if the checkbox is set

Related Questions