Skip to content

Commit

Permalink
Fix #13: Modification of the Formex4 builder to better deal with empt…
Browse files Browse the repository at this point in the history
…y cells (management of ``<IE/>`` tags).
  • Loading branch information
laurent-laporte-pro committed Nov 10, 2021
1 parent 2c40624 commit 8568173
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1,051 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -21,6 +21,8 @@ ooxml2formex4 conversion – Loss of image calls in table conversion.

* Modification of the OOXML parser to improve empty cells detection for Formex4 conversion (``<IE/>`` tags management).

* Modification of the Formex4 builder to better deal with empty cells (management of ``<IE/>`` tags).


v0.4.3 (2019-10-15)
===================
Expand Down
35 changes: 23 additions & 12 deletions benker/builders/formex4.py
Expand Up @@ -185,11 +185,20 @@ def build_title(self, tbl_elem, row):
:type row: benker.table.RowView
:param row: The row which contains the title.
.. versionchanged:: 0.4.4
Modification of the Formex4 builder to better deal with empty cells (management of ``<IE/>`` tags).
"""
title_elem = etree.SubElement(tbl_elem, u"TITLE")
for cell in row.owned_cells:
text = text_type(cell)
if text:
# When a cell is empty, we need to insert the ``<IE/>`` tag.
is_empty_cell = cell.styles.get("x-cell-empty", "false") == "true"
# We can also have an empty content if a short row is completed by empty cells.
if is_empty_cell or cell.content is None or cell.content == "":
# assert cell.content in {None, "", []}
ti_elem = etree.SubElement(title_elem, u"TI")
etree.SubElement(ti_elem, u"IE")
else:
if isinstance(cell.content, type(u"")):
# mainly useful for unit test
ti_elem = etree.SubElement(title_elem, u"TI")
Expand All @@ -201,10 +210,6 @@ def build_title(self, tbl_elem, row):
ti_elem.append(paragraphs[0])
sti_elem = etree.SubElement(title_elem, u"STI")
sti_elem.extend(paragraphs[1:])
else:
# assert cell.content in {None, "", []}
ti_elem = etree.SubElement(title_elem, u"TI")
etree.SubElement(ti_elem, u"IE")

def build_row(self, corpus_elem, row):
"""
Expand Down Expand Up @@ -314,6 +319,9 @@ def build_cell(self, row_elem, cell, row):
:type row: benker.table.RowView
:param row: The parent row.
.. versionchanged:: 0.4.4
Modification of the Formex4 builder to better deal with empty cells (management of ``<IE/>`` tags).
"""
# cell_styles = cell.styles
attrs = {'COL': str(cell.box.min.x)}
Expand All @@ -325,17 +333,20 @@ def build_cell(self, row_elem, cell, row):
if cell.height > 1:
attrs[u"ROWSPAN"] = str(cell.height)
cell_elem = etree.SubElement(row_elem, u"CELL", attrib=attrs)
text = text_type(cell)
if text:

# When a cell is empty, we need to insert the ``<IE/>`` tag.
is_empty_cell = cell.styles.get("x-cell-empty", "false") == "true"
# We can also have an empty content if a short row is completed by empty cells.
if is_empty_cell or cell.content is None or cell.content == "":
# The IE element is used to explicitly indicate
# that specific structures have an empty content.
etree.SubElement(cell_elem, u"IE")
else:
if isinstance(cell.content, type(u"")):
# mainly useful for unit test
cell_elem.text = cell.content
else:
cell_elem.extend(cell.content)
else:
# The IE element is used to explicitly indicate
# that specific structures have an empty content.
etree.SubElement(cell_elem, u"IE")

def finalize_tree(self, tree):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/builders/test_formex4_builder.py
Expand Up @@ -236,7 +236,7 @@ def test_build_tbl__with_title():
table = Table()
table.rows[1].insert_cell([P(u"1 euro =")], width=3, styles={"align": "center"})
table.rows[2].nature = "head"
table.rows[2].insert_cell([P()])
table.rows[2].insert_cell([P()], styles={"x-cell-empty": "true"})
table.rows[2].insert_cell([P(u"Currency")])
table.rows[2].insert_cell([P(u"Exchange rate")])
table.rows[3].insert_cell([P(u"USD")])
Expand Down

0 comments on commit 8568173

Please sign in to comment.