diff options
| -rw-r--r-- | mammoth/documents.py | 9 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 36 |
2 files changed, 26 insertions, 19 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index f782d32..f9c0665 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -86,6 +86,13 @@ class TableCell(HasChildren): rowspan = cobble.field() @cobble.data +class TableCellUnmerged: + children = cobble.field() + colspan = cobble.field() + rowspan = cobble.field() + vmerge = cobble.field() + +@cobble.data class Break(Element): break_type = cobble.field() @@ -194,6 +201,8 @@ def table_cell(children, colspan=None, rowspan=None): rowspan = 1 return TableCell(children=children, colspan=colspan, rowspan=rowspan) +def table_cell_unmerged(children, colspan, rowspan, vmerge): + return TableCellUnmerged(children=children, colspan=colspan, rowspan=rowspan, vmerge=vmerge) def numbering_level(level_index, is_ordered): return _NumberingLevel(str(level_index), bool(is_ordered)) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 40b5f9d..3de2c60 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -370,12 +370,11 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f colspan = int(gridspan) return _read_xml_elements(element.children) \ - .map(lambda children: _add_attrs( - documents.table_cell( - children=children, - colspan=colspan - ), - _vmerge=read_vmerge(properties), + .map(lambda children: documents.table_cell_unmerged( + children=children, + colspan=colspan, + rowspan=1, + vmerge=read_vmerge(properties), )) def read_vmerge(properties): @@ -398,7 +397,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f )]) unexpected_non_cells = any( - not isinstance(cell, documents.TableCell) + not isinstance(cell, documents.TableCellUnmerged) for row in rows for cell in row.children ) @@ -411,17 +410,23 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f for row in rows: cell_index = 0 for cell in row.children: - if cell._vmerge and cell_index in columns: + if cell.vmerge and cell_index in columns: columns[cell_index].rowspan += 1 else: columns[cell_index] = cell - cell._vmerge = False + cell.vmerge = False cell_index += cell.colspan for row in rows: - row.children = lists.filter(lambda cell: not cell._vmerge, row.children) - for cell in row.children: - del cell._vmerge + row.children = [ + documents.table_cell( + children=cell.children, + colspan=cell.colspan, + rowspan=cell.rowspan, + ) + for cell in row.children + if not cell.vmerge + ] return _success(rows) @@ -716,13 +721,6 @@ def _concat(*values): return result -def _add_attrs(obj, **kwargs): - for key, value in kwargs.items(): - setattr(obj, key, value) - - return obj - - def _is_int(value): if value is None: return False |
