aboutsummaryrefslogtreecommitdiff
path: root/mammoth
diff options
context:
space:
mode:
Diffstat (limited to 'mammoth')
-rw-r--r--mammoth/__init__.py15
-rw-r--r--mammoth/docx/__init__.py46
-rw-r--r--mammoth/style_reader/document_matcher_reader.py2
-rw-r--r--mammoth/style_reader/html_path_reader.py6
4 files changed, 59 insertions, 10 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 92278f7..3c212ac 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -1,6 +1,17 @@
from .results import Result
-from . import docx, conversion
+from . import docx, conversion, style_reader
def convert_to_html(fileobj):
- return docx.read(fileobj).bind(conversion.convert_document_element_to_html)
+ return docx.read(fileobj).bind(lambda document:
+ conversion.convert_document_element_to_html(document, styles=_create_default_styles())
+ )
+
+
+def _create_default_styles():
+ lines = filter(None, map(lambda line: line.strip(), _default_styles.split("\n")))
+ return map(style_reader.read_style, lines)
+
+_default_styles = """
+p:unordered-list(1) => ul > li:fresh
+"""
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py
index bb26826..de15b78 100644
--- a/mammoth/docx/__init__.py
+++ b/mammoth/docx/__init__.py
@@ -3,16 +3,54 @@ import zipfile
from ..results import Result
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
+from .numbering_xml import read_numbering_xml_element, Numbering
_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"),
]
-
def read(fileobj):
zip_file = zipfile.ZipFile(fileobj)
- document_fileobj = zip_file.open("word/document.xml")
- document_xml = parse_xml(document_fileobj, _namespaces)
- return read_document_xml_element(document_xml)
+
+ with zip_file.open("[Content_Types].xml") as content_types_fileobj:
+ content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj))
+
+ with zip_file.open("word/_rels/document.xml.rels") as relationships_fileobj:
+ relationships = read_relationships_xml_element(_parse_docx_xml(relationships_fileobj))
+
+ if _has_entry(zip_file, "word/numbering.xml"):
+ with zip_file.open("word/numbering.xml") as numbering_fileobj:
+ numbering = read_numbering_xml_element(_parse_docx_xml(numbering_fileobj))
+ else:
+ numbering = Numbering({})
+
+ with zip_file.open("word/document.xml") as document_fileobj:
+ document_xml = _parse_docx_xml(document_fileobj)
+ return read_document_xml_element(
+ document_xml,
+ content_types=content_types,
+ relationships=relationships,
+ docx_file=zip_file,
+ numbering=numbering,
+ )
+
+
+def _parse_docx_xml(fileobj):
+ return parse_xml(fileobj, _namespaces)
+
+def _has_entry(zip_file, name):
+ try:
+ zip_file.getinfo(name)
+ return True
+ except KeyError:
+ return False
diff --git a/mammoth/style_reader/document_matcher_reader.py b/mammoth/style_reader/document_matcher_reader.py
index 4bc7247..44a9896 100644
--- a/mammoth/style_reader/document_matcher_reader.py
+++ b/mammoth/style_reader/document_matcher_reader.py
@@ -37,7 +37,7 @@ def _read_style_node(style_node):
def _read_list_node(list_node):
if list_node.children:
return documents.numbering_level(
- int(list_node.children[0].children[3].text),
+ int(list_node.children[0].children[3].text) - 1,
is_ordered=list_node.children[0].children[1].text == "ordered-list",
)
else:
diff --git a/mammoth/style_reader/html_path_reader.py b/mammoth/style_reader/html_path_reader.py
index 0f1331b..b508f59 100644
--- a/mammoth/style_reader/html_path_reader.py
+++ b/mammoth/style_reader/html_path_reader.py
@@ -49,18 +49,18 @@ def _repeated_children_with_separator(node, has_whitespace):
yield node.children[0]
if has_whitespace:
- sequence_node_index = 2
+ sequence_node_index = 3
else:
sequence_node_index = 1
- sequence_node = node.children[sequence_node_index]
+ sequence_node = node.children[1]
for child in sequence_node.children:
yield child.children[sequence_node_index]
grammar_text = r"""
-html_path = element whitespace* (">" whitespace* element)*
+html_path = element (whitespace* ">" whitespace* element)*
element = tag_names class_name* fresh?