Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

Changelog
=========
* 0.3.3
* In the event that `cElementTree` has a problem parsing the document, a
`MalformedDocxException` is raised instead of a `SyntaxError`
* 0.3.2
* We were not taking into account that vertical merges should have a
continue attribute, but sometimes they do not, and in those cases word
Expand Down
2 changes: 2 additions & 0 deletions pydocx/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MalformedDocxException(Exception):
pass
15 changes: 14 additions & 1 deletion pydocx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections import defaultdict
from xml.etree import cElementTree

from pydocx.exceptions import MalformedDocxException


UPPER_ROMAN_TO_HEADING_VALUE = 'h2'
TAGS_CONTAINING_CONTENT = (
Expand Down Expand Up @@ -70,6 +72,14 @@ def _filter_children(element, tags):


def remove_namespaces(document):
"""
>>> exception_raised = False
>>> try:
... remove_namespaces('junk')
... except MalformedDocxException:
... exception_raised = True
>>> assert exception_raised
"""
encoding_regex = re.compile(
r'<\?xml.*encoding="(.+?)"',
re.DOTALL | re.MULTILINE,
Expand All @@ -78,7 +88,10 @@ def remove_namespaces(document):
m = encoding_regex.match(document)
if m:
encoding = m.groups(0)[0]
root = cElementTree.fromstring(document)
try:
root = cElementTree.fromstring(document)
except SyntaxError:
raise MalformedDocxException('This document cannot be converted.')
for child in el_iter(root):
child.tag = child.tag.split("}")[1]
child.attrib = dict(
Expand Down