diff --git a/html4docx/h4d.py b/html4docx/h4d.py index 5184e16..e1dd1e8 100644 --- a/html4docx/h4d.py +++ b/html4docx/h4d.py @@ -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( diff --git a/tests/test_h4d.py b/tests/test_h4d.py index 8b510a9..5e851c0 100644 --- a/tests/test_h4d.py +++ b/tests/test_h4d.py @@ -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 = '
Test
' + + doc = Document() + parser = HtmlToDocx() + parser.add_html_to_document(html, self.document) + parser.add_html_to_document(html, doc) + if __name__ == "__main__": unittest.main()