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.8
* If zipfile fails to open the passed in file, we are now raising a
`MalformedDocxException` instead of a `BadZipFIle`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document this. How about a README section called exceptions that at least documents this exception?

* 0.3.7
* Some inline tags (most notably the underline tag) could have a `val` of
`none` and that would signify that the style is disabled. A `val` of
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ The base parser `Docx2Html` relies on certain css class being set for certain be
* class `pydocx-strike` -> Strike a line through.
* class `pydocx-hidden` -> Hide the text.

Exceptions
##########

Right now there is only one custom exception (`MalformedDocxException`). It is raised if either the `xml` or `zipfile` libraries raise an exception.

Optional Arguments
##################

Expand Down
6 changes: 5 additions & 1 deletion pydocx/DocxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
find_ancestor_with_tag,
has_descendant_with_tag,
)
from pydocx.exceptions import MalformedDocxException

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("NewParser")
Expand All @@ -37,7 +38,10 @@

@contextmanager
def ZipFile(path): # This is not needed in python 3.2+
f = zipfile.ZipFile(path)
try:
f = zipfile.ZipFile(path)
except zipfile.BadZipfile:
raise MalformedDocxException('Passed in document is not a docx')
yield f
f.close()

Expand Down
10 changes: 9 additions & 1 deletion pydocx/tests/test_docx.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import base64

from os import path
from tempfile import NamedTemporaryFile

from nose.plugins.skip import SkipTest
from nose.tools import raises

from pydocx.tests import assert_html_equal, BASE_HTML
from pydocx.parsers.Docx2Html import Docx2Html
from pydocx.DocxParser import ZipFile
from pydocx.exceptions import MalformedDocxException


def convert(path, *args, **kwargs):
Expand Down Expand Up @@ -749,6 +751,12 @@ def test_missing_numbering():
''')


@raises(MalformedDocxException)
def test_malformed_docx_exception():
with NamedTemporaryFile(suffix='.docx') as f:
convert(f.name)


def _converter(*args, **kwargs):
# Having a converter that does nothing is the same as if abiword fails to
# convert.
Expand Down