I have tables created in Excel that I am converting to HTML. The tables have merged cells, which I'm trying to maintain in the HTML. Using the answer at this link, I am able to get the rowspan for columns with merged rows. I'm now trying to do the same for rows that are merged (colspan).
For example, in the table below, row 5 should be one merged cell (image shows the table in Excel).
I feel like I'm about 90% of the way there, but my code keeps wanting to merge the two "no" cells on rows 3 and 4 (image shows the table after running the code and copying back into Excel).
I figured I could run the table through a loop for each row, but I can't quite figure out how to structure my loop.
When I run the first set of code below, row 5 merges as intended. But when I run the second set of code, my two "no" cells merge.
(When I tested out "for cols in rows[4:5]:" I get the following error: TypeError: 'NoneType' object does not support item assignment)
Any advice on where I'm going wrong? Hopefully I've copied all the relevant code for the sample.
number_of_rows = len(tbl2.findAll('tr'))
rows = tbl2.findAll("tr")
for cols in rows[4:5]:
for col_cell in cols.findAll('td'):
this = col_cell
if this.has_attr("rowspan"):
this = col_cell
elif row_last and this.text == row_last.text:
this.decompose()
if 'colspan' not in row_last:
row_last['colspan'] = 1
row_last['colspan'] += 1
else:
row_last = this
number_of_rows = len(tbl2.findAll('tr'))
rows = tbl2.findAll("tr")
# Instead of going through the table line by line, the code below goes through the
# entire table i times (where i is however many rows there are in the table).
# I've tried replacing the first two lines with "for cols in enumerate(rows):"
# but end up with the following error:
# AttributeError: 'tuple' object has no attribute 'findAll'
for i in range(number_of_rows):
for cols in rows[i:i+1]:
for col_cell in cols.findAll('td'):
this = col_cell
if this.has_attr("rowspan"):
this = col_cell
elif row_last and this.text == row_last.text:
this.decompose()
if 'colspan' not in row_last:
row_last['colspan'] = 1
row_last['colspan'] += 1
else:
row_last = this