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: 7 additions & 3 deletions htmldocx/h2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,13 @@ def get_table_dimensions(self, table_soup):
rows = self.get_table_rows(table_soup)
# Table is either empty or has non-direct children between table and tr tags
# Thus the row dimensions and column dimensions are assumed to be 0

cols = self.get_table_columns(rows[0]) if rows else []
return len(rows), len(cols)
# A table can have a varying number of columns per row,
# so it is important to find the maximum number of columns in any row
if rows:
cols = max(len(self.get_table_columns(row)) for row in rows)
else:
cols = 0
return len(rows), cols

def get_tables(self):
if not hasattr(self, 'soup'):
Expand Down
13 changes: 13 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ def test_handling_hr(self):
)
self.parser.add_html_to_document("<p>paragraph</p><hr><p>paragraph</p>", self.document)

def test_unbalanced_table(self):
# A table with more td elements in latter rows than in the first
self.document.add_heading(
'Test: Handling unbalanced tables',
level=1
)
self.parser.add_html_to_document(
"<table>"
"<tr><td>Hello</td></tr>"
"<tr><td>One</td><td>Two</td></tr>"
"</table>",
self.document)


if __name__ == '__main__':
unittest.main()