diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-07-06 17:29:23 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-07-06 17:29:23 +0100 |
| commit | 6c0c5a0aea77387ea4873bee27e8df88f867002b (patch) | |
| tree | a960b20643ede28e7c38556c8fc8bd265398a704 | |
| parent | 9bfd191c00cf0d6f96eaf4e452cecadab711239e (diff) | |
Add conversion from document table to HTML table
| -rw-r--r-- | mammoth/conversion.py | 27 | ||||
| -rw-r--r-- | mammoth/documents.py | 7 | ||||
| -rw-r--r-- | tests/conversion_tests.py | 25 |
3 files changed, 58 insertions, 1 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index a21a264..fa1333b 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -26,6 +26,9 @@ class DocumentConverter(object): documents.Text: self._convert_text, documents.Hyperlink: self._convert_hyperlink, documents.Tab: self._convert_tab, + documents.Table: self._convert_table, + documents.TableRow: self._convert_table_row, + documents.TableCell: self._convert_table_cell, documents.Image: convert_image or self._convert_image, } @@ -72,6 +75,30 @@ class DocumentConverter(object): html_generator.text("\t") + def _convert_table(self, table, html_generator): + html_generator.end_all() + html_generator.start("table") + self._convert_elements_to_html(table.children, html_generator) + html_generator.end() + + + def _convert_table_row(self, table_row, html_generator): + html_generator.start("tr") + self._convert_elements_to_html(table_row.children, html_generator) + html_generator.end() + + + def _convert_table_cell(self, table_cell, html_generator): + html_generator.start("td") + for child in table_cell.children: + child_generator = HtmlGenerator() + self.convert_element_to_html(child, child_generator) + child_generator.end_all() + html_generator.append(child_generator) + + html_generator.end() + + def _convert_image(self, image, html_generator): with image.open() as image_bytes: encoded_src = base64.b64encode(image_bytes.read()).decode("ascii") diff --git a/mammoth/documents.py b/mammoth/documents.py index 703e436..7f2750c 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -6,6 +6,9 @@ Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "style_name", Run = dodge.data_class("Run", ["children", "style_id", "style_name", "is_bold", "is_italic"]) Text = dodge.data_class("Text", ["value"]) Hyperlink = dodge.data_class("Hyperlink", ["href", "children"]) +Table = dodge.data_class("Table", ["children"]) +TableRow = dodge.data_class("TableRow", ["children"]) +TableCell = dodge.data_class("TableCell", ["children"]) class Tab(object): pass @@ -36,7 +39,9 @@ def tab(): image = Image hyperlink = Hyperlink - +table = Table +table_row = TableRow +table_cell = TableCell def numbering_level(level_index, is_ordered): return _NumberingLevel(str(level_index), bool(is_ordered)) diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index acac53b..3c5ff88 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -171,6 +171,27 @@ def docx_tab_is_converted_to_tab_in_html(): @istest +def docx_table_is_converted_to_table_in_html(): + table = documents.table([ + documents.table_row([ + documents.table_cell([_paragraph_with_text("Top left")]), + documents.table_cell([_paragraph_with_text("Top right")]), + ]), + documents.table_row([ + documents.table_cell([_paragraph_with_text("Bottom left")]), + documents.table_cell([_paragraph_with_text("Bottom right")]), + ]), + ]) + result = convert_document_element_to_html(table) + expected_html = ( + "<table>" + + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + + "</table>") + assert_equal(expected_html, result.value) + + +@istest def images_are_converted_to_img_tags_with_data_uri(): image = documents.image(alt_text=None, content_type="image/png", open=lambda: io.BytesIO(b"abc")) result = convert_document_element_to_html(image) @@ -196,5 +217,9 @@ def can_define_custom_conversion_for_images(): assert_equal('<img alt="abc" />', result.value) +def _paragraph_with_text(text): + return documents.paragraph(children=[_run_with_text(text)]) + + def _run_with_text(text): return documents.run(children=[documents.text(text)]) |
