Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Throw an exception if the docx file is invalid. #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,19 @@
'dcmitype': 'http://purl.org/dc/dcmitype/',
'dcterms': 'http://purl.org/dc/terms/'}

class BaseDocxError(Exception):
pass

class BadDocxFile(BaseDocxError):
pass

def opendocx(file):
'''Open a docx file, return a document XML tree'''
mydoc = zipfile.ZipFile(file)
xmlcontent = mydoc.read('word/document.xml')
try:
mydoc = zipfile.ZipFile(file)
xmlcontent = mydoc.read('word/document.xml')
except (zipfile.BadZipfile, KeyError):
raise BadDocxFile("File is not a docx file")
document = etree.fromstring(xmlcontent)
return document

Expand Down