From 68047ef381cb08cf43e5a30ca9dd6e40ccde8cdd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 1 Dec 2013 01:15:25 +0000 Subject: Add basic conversion to HTML --- mammoth/__init__.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'mammoth/__init__.py') diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 803273f..195e818 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,5 +1,46 @@ from .results import Result +from . import docx, documents def convert_to_html(fileobj): - return Result("", []) + document = docx.read(fileobj).value + html = _convert_element_to_html(document) + return Result(html, []) + + +def _convert_element_to_html(element): + return _converters[type(element)](element) + + +_converters = {} + + +def converter(element_type): + def add(func): + _converters[element_type] = func + + return add + + +@converter(documents.Document) +def convert_document(document): + return _convert_elements_to_html(document.children) + + +@converter(documents.Paragraph) +def convert_paragraph(paragraph): + return "

{0}

".format(_convert_elements_to_html(paragraph.children)) + + +@converter(documents.Run) +def convert_run(run): + return _convert_elements_to_html(run.children) + + +@converter(documents.Text) +def convert_text(text): + return text.value + + +def _convert_elements_to_html(elements): + return "".join(map(_convert_element_to_html, elements)) -- cgit v1.2.3