Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions html4docx/h4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,8 +1295,14 @@ def handle_table(self, current_attrs):

# Reference:
# https://python-docx.readthedocs.io/en/latest/dev/analysis/features/table/cell-merge.html
rowspan = int(col.get('rowspan', 1))
colspan = int(col.get('colspan', 1))
try:
rowspan = int(col.get('rowspan', 1))
except ValueError:
rowspan = 1
try:
colspan = int(col.get('colspan', 1))
except ValueError:
colspan = 1

if rowspan > 1 or colspan > 1:
docx_cell = docx_cell.merge(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_h4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2573,5 +2573,15 @@ def test_invalid_color_fallback_to_black(self):
self.assertIn('Could not parse color \'invalidcolorname\': Invalid color value. Fallback to black.', log.output[3])
self.assertIn('Could not parse color \'#f7272626161\': Invalid color value. Fallback to black.', log.output[4])

def test_invalid_rowspan_and_colspan(self):
"""Test with invalid rowspan and colspan"""
self.document.add_heading("Test: Test invalid rowspan and colspan", level=1)
html = '<table><tr><td rowspan="invalid">Test</td></tr></table>'

doc = Document()
parser = HtmlToDocx()
parser.add_html_to_document(html, self.document)
parser.add_html_to_document(html, doc)

if __name__ == "__main__":
unittest.main()
Loading