From 5a5112f8c0286da6875e54b2e61a32288135d1e8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 26 Dec 2015 10:50:25 +0000 Subject: Split off office_xml module --- mammoth/docx/__init__.py | 28 +++++----------------------- mammoth/docx/office_xml.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 mammoth/docx/office_xml.py diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index af447b7..d944a2d 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -1,7 +1,6 @@ import os from .. import results, lists -from .xmlparser import parse_xml from .document_xml import read_document_xml_element from .content_types_xml import read_content_types_xml_element from .relationships_xml import read_relationships_xml_element, Relationships @@ -9,23 +8,10 @@ from .numbering_xml import read_numbering_xml_element, Numbering from .styles_xml import read_styles_xml_element from .notes_xml import create_footnotes_reader, create_endnotes_reader from .files import Files -from . import body_xml +from . import body_xml, office_xml from ..zips import open_zip -_namespaces = [ - ("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), - ("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"), - ("a", "http://schemas.openxmlformats.org/drawingml/2006/main"), - ("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture"), - ("content-types", "http://schemas.openxmlformats.org/package/2006/content-types"), - ("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"), - ("relationships", "http://schemas.openxmlformats.org/package/2006/relationships"), - ("v", "urn:schemas-microsoft-com:vml"), - ("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"), - ("office-word", "urn:schemas-microsoft-com:office:word"), -] - def read(fileobj): zip_file = open_zip(fileobj, "r") body_readers = _body_readers(getattr(fileobj, "name", None), zip_file) @@ -49,7 +35,7 @@ def _read_notes(zip_file, body_readers): def _read_document(zip_file, body_readers, notes): with zip_file.open("word/document.xml") as document_fileobj: - document_xml = _parse_docx_xml(document_fileobj) + document_xml = office_xml.read(document_fileobj) return read_document_xml_element( document_xml, body_reader=body_readers("document"), @@ -59,13 +45,13 @@ def _read_document(zip_file, body_readers, notes): def _body_readers(document_path, zip_file): with zip_file.open("[Content_Types].xml") as content_types_fileobj: - content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj)) + content_types = read_content_types_xml_element(office_xml.read(content_types_fileobj)) numbering = _try_read_entry_or_default( zip_file, "word/numbering.xml", read_numbering_xml_element, default=Numbering({})) with zip_file.open("word/styles.xml") as styles_fileobj: - styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj)) + styles = read_styles_xml_element(office_xml.read(styles_fileobj)) def for_name(name): relationships_path = "word/_rels/{0}.xml.rels".format(name) @@ -85,13 +71,9 @@ def _body_readers(document_path, zip_file): return for_name -def _parse_docx_xml(fileobj): - return parse_xml(fileobj, _namespaces) - - def _try_read_entry_or_default(zip_file, name, reader, default): if zip_file.exists(name): with zip_file.open(name) as fileobj: - return reader(_parse_docx_xml(fileobj)) + return reader(office_xml.read(fileobj)) else: return default diff --git a/mammoth/docx/office_xml.py b/mammoth/docx/office_xml.py new file mode 100644 index 0000000..4dffb44 --- /dev/null +++ b/mammoth/docx/office_xml.py @@ -0,0 +1,20 @@ +from .xmlparser import parse_xml + + +_namespaces = [ + ("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), + ("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"), + ("a", "http://schemas.openxmlformats.org/drawingml/2006/main"), + ("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture"), + ("content-types", "http://schemas.openxmlformats.org/package/2006/content-types"), + ("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"), + ("relationships", "http://schemas.openxmlformats.org/package/2006/relationships"), + ("v", "urn:schemas-microsoft-com:vml"), + ("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"), + ("office-word", "urn:schemas-microsoft-com:office:word"), +] + + +def read(fileobj): + return parse_xml(fileobj, _namespaces) + -- cgit v1.2.3