You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a sample document in Word and the namespaces contained within it seem to be different than those that are natively included with python-docx. I found the included namespace map here.
And it appears that the newest version overrides the XPath function and uses these included namespaces. In previous versions of python-docx I would use the child's namespace directly in the function to ensure that it was found, but with this override I am unable to do it and get the error:
TypeError: xpath() got an unexpected keyword argument 'namespaces'
from docx import Document
doc = Document('checkbox_control.docx')
for paragraph in doc.paragraphs:
checkboxes = paragraph._element.xpath('w14:checkBox')
# Old method that no longer works
# checkboxes = paragraph._element.xpath('w14:checkBox', namespaces=paragraph._element.nsmap)
Being able to get this functionality back would be great as I am using python-docx to parse through some Word documents with a lot more ease than using lxml/etc for the entire document.
I found a solution that works for now, but is quite hacky:
from docx import Document
from lxml import etree
doc = Document('checkbox_control.docx')
for paragraph in doc.paragraphs:
checkboxes = etree.ElementBase.xpath(paragraph._element, './/w14:checkbox', namespaces=paragraph._element.nsmap)
# Old method that no longer works
# checkboxes = paragraph._element.xpath('w14:checkBox', namespaces=paragraph._element.nsmap)
Reacted by BriceLeMoing, nponcian-rootsauto and Alexey
Activity
fdabek1 commentedon Feb 27, 2019
I found a solution that works for now, but is quite hacky:
scanny commentedon May 16, 2026
Closed as inactive.