unstructured icon indicating copy to clipboard operation
unstructured copied to clipboard

bug/partition_html fails when parsing html with tables with no tbody

Open dgunning opened this issue 3 years ago • 3 comments

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

SEC Form 3

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.

Table with no table body

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

dgunning avatar Oct 20 '23 00:10 dgunning

Hi @dgunning,

Thanks for submitting this issue. We are tracking the issue and will review it shortly.

badGarnet avatar Oct 20 '23 16:10 badGarnet

This change should work. If the table has no body then the HTMLTable text would be ""

                if body:
                    rows = body.findall("tr")
                else:
                    rows = []

dgunning avatar Oct 20 '23 17:10 dgunning

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

badGarnet avatar Oct 20 '23 18:10 badGarnet