From b249ef56971405e7b1d0b4bb3b9cfb4e779583ab Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Jul 2016 15:26:15 +0100 Subject: Wire up code for converting comments --- mammoth/docx/__init__.py | 31 ++++++++++++++++++++++++------- mammoth/docx/body_xml.py | 2 +- mammoth/docx/document_xml.py | 14 +++++++++++--- 3 files changed, 36 insertions(+), 11 deletions(-) (limited to 'mammoth/docx') diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 47e7a21..8fb150a 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -7,39 +7,56 @@ from .relationships_xml import read_relationships_xml_element, Relationships 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 .comments_xml import create_comments_reader from .files import Files from . import body_xml, office_xml from ..zips import open_zip +_empty_result = results.success([]) + + def read(fileobj): zip_file = open_zip(fileobj, "r") body_readers = _body_readers(getattr(fileobj, "name", None), zip_file) - return _read_notes(zip_file, body_readers).bind(lambda notes: - _read_document(zip_file, body_readers, notes)) + return results.combine([ + _read_notes(zip_file, body_readers), + _read_comments(zip_file, body_readers), + ]).bind(lambda referents: + _read_document(zip_file, body_readers, notes=referents[0], comments=referents[1]) + ) def _read_notes(zip_file, body_readers): - empty_result = results.success([]) - read_footnotes_xml = create_footnotes_reader(body_readers("footnotes")) footnotes = _try_read_entry_or_default( - zip_file, "word/footnotes.xml", read_footnotes_xml, default=empty_result) + zip_file, "word/footnotes.xml", read_footnotes_xml, default=_empty_result) read_endnotes_xml = create_endnotes_reader(body_readers("endnotes")) endnotes = _try_read_entry_or_default( - zip_file, "word/endnotes.xml", read_endnotes_xml, default=empty_result) + zip_file, "word/endnotes.xml", read_endnotes_xml, default=_empty_result) return results.combine([footnotes, endnotes]).map(lists.flatten) + + +def _read_comments(zip_file, body_readers): + return _try_read_entry_or_default( + zip_file, + "word/comments.xml", + create_comments_reader(body_readers("comments")), + default=_empty_result, + ) + -def _read_document(zip_file, body_readers, notes): +def _read_document(zip_file, body_readers, notes, comments): with zip_file.open("word/document.xml") as document_fileobj: document_xml = office_xml.read(document_fileobj) return read_document_xml_element( document_xml, body_reader=body_readers("document"), notes=notes, + comments=comments, ) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index fe75f7c..b1be2e5 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -52,13 +52,13 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f "office-word:wrap", "v:shadow", "v:shapetype", + "w:annotationRef", "w:bookmarkEnd", "w:sectPr", "w:proofErr", "w:lastRenderedPageBreak", "w:commentRangeStart", "w:commentRangeEnd", - "w:commentReference", "w:del", "w:footnoteRef", "w:endnoteRef", diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index 1c15001..e0d19a4 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -1,13 +1,21 @@ from .. import documents -def read_document_xml_element(element, +def read_document_xml_element( + element, body_reader, - notes=None): + notes=None, + comments=None): if notes is None: notes = [] + if comments is None: + comments = [] body_element = element.find_child("w:body") return body_reader.read_all(body_element.children) \ - .map(lambda children: documents.document(children, documents.notes(notes))) + .map(lambda children: documents.document( + children, + notes=documents.notes(notes), + comments=comments + )) -- cgit v1.2.3