Skip to content

Instantly share code, notes, and snippets.

@Winand
Last active August 1, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Winand/3ac7ea98fdfdcc78392c6793d9d8b776 to your computer and use it in GitHub Desktop.
Save Winand/3ac7ea98fdfdcc78392c6793d9d8b776 to your computer and use it in GitHub Desktop.
find column respecting row/colspans
# 0-based /row/ to search for /text/
def find_column(table, row, text):
trs = table.find_all('tr')
results = [[c for c in trs[i].findChildren(recursive=False)]
for i in range(0, row+1)]
for r in range(row-1, -1, -1):
i = 0
for c in results[r]:
if r+int(c.get('rowspan', 0)) > row:
results[-1].insert(i, None)
i += int(c.get('colspan', 1))
i = 0
for c in results[-1]:
if c and c.text.strip() == text:
return i
i += int(c.get('colspan', 1) if c else 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment