Skip to content

fix: escape style names to allow embedded quotes (and other XPath characters) #246

@incredibleharry

Description

@incredibleharry

These seem to cause errors when referencing them (but not when adding them)

For example:
Body Text First Indent 2"
Hang.5"

for sy in toReview.styles:
newstyle = outputDoc_styles.add_style(sy.name, sy.type) ##this works
newfont = outputDoc_styles[sy.name].font ##this crashes

Traceback (most recent call last):
...
File "/usr/local/lib/python3.4/dist-packages/docx/styles/styles.py", line 44, in getitem
style_elm = self._element.get_by_name(BabelFish.ui2internal(key))
File "/usr/local/lib/python3.4/dist-packages/docx/oxml/styles.py", line 343, in get_by_name
return self.xpath(xpath)[0]
File "/usr/local/lib/python3.4/dist-packages/docx/oxml/xmlchemy.py", line 751, in xpath
xpath_str, namespaces=nsmap
File "lxml.etree.pyx", line 1507, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:52198)
File "xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.call (src/lxml/lxml.etree.c:152124)
File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:151097)
File "xpath.pxi", line 212, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:150896)
lxml.etree.XPathEvalError: Invalid predicate

Anyways, I can work around this by changing the " to a ' with str.replace but maybe this is something that is easily fixed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions