bug/partition_html fails when parsing html with tables with no tbody
Describe the bug partition_html fails when parsing tables with a thead but no tbody.
Inside _process_leaf_table_item(tag_elem) the following code fails with
AttributeError: 'NoneType' object has no attribute 'findall'
if tag_elem.tag in TABLE_TAGS:
nested_table = tag_elem.findall("table")
if not nested_table:
rows = tag_elem.findall("tr")
if not rows:
body = tag_elem.find("tbody")
rows = body.findall("tr")
This happens because the table does not have a tbody. The table has a thead that contains th cells with values.
This happens in SEC Form 3,4 and 5 where the thead contains the headers for optional data that is not present in the table. The table header is still present to show the structure of the table
To Reproduce
from unstructured.partition.html import partition_html
table_html = """
<table>
<thead>
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
</table>
"""
partitions = partition_html(text=table_html)
Expected behavior partitionl_html returns a list of partitions. The table with the missing table body should still be returned as a HTMLTable
Screenshots If applicable, add screenshots to help explain your problem.
Environment Info OS version: macOS-13.3.1-x86_64-i386-64bit Python version: 3.10.10 unstructured version: None unstructured-inference version: 0.7.7 pytesseract version: 0.3.10 Torch version: 2.1.0 Detectron2 is not installed
Additional context I am the author of edgartools which uses unstructured to partition SEC filing html
Hi @dgunning,
Thanks for submitting this issue. We are tracking the issue and will review it shortly.
This change should work. If the table has no body then the HTMLTable text would be ""
if body:
rows = body.findall("tr")
else:
rows = []
This change should work. If the table has no body then the HTMLTable text would be ""
if body: rows = body.findall("tr") else: rows = []
please take a look at the linked PR; it is intended to find any section tagged by thead as tables as well