1

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).

sample table

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).

sample table 2

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
mvCode
  • 25
  • 5
  • we don't have your data to run code and see what is the problem. I can only suggerst to use `print()` to see which part of code is executed and what you have in variables. It is called `"print debuging"` and it helps to see problem. – furas Feb 28 '22 at 22:08
  • @furas I'm not sure the best way to add more code/data without making the question unreadable. That said, I added some print() items and found out that the second set of code is going through the table repeatedly instead of line by line like I hoped. I've updated the second set of code with comments indicating what I've tried (unsuccessfully) to correct the issue. – mvCode Mar 01 '22 at 15:10

0 Answers0