Parse the content of a table tag. Args: The table element. Returns: A docling table object.
(self, table: Tag)
| 1524 | return ncols_max |
| 1525 | |
| 1526 | def _parse_table(self, table: Tag) -> TableData: |
| 1527 | """Parse the content of a table tag. |
| 1528 | |
| 1529 | Args: |
| 1530 | The table element. |
| 1531 | |
| 1532 | Returns: |
| 1533 | A docling table object. |
| 1534 | """ |
| 1535 | tgs_align = [] |
| 1536 | tg_secs = table.find_all("tgroup") |
| 1537 | if tg_secs: |
| 1538 | for tg_sec in tg_secs: |
| 1539 | ncols = tg_sec.get("cols", None) |
| 1540 | if ncols: |
| 1541 | ncols = int(ncols) |
| 1542 | tg_align = {"ncols": ncols, "colinfo": []} |
| 1543 | cs_secs = tg_sec.find_all("colspec") |
| 1544 | if cs_secs: |
| 1545 | for cs_sec in cs_secs: |
| 1546 | colname = cs_sec.get("colname", None) |
| 1547 | colwidth = cs_sec.get("colwidth", None) |
| 1548 | tg_align["colinfo"].append( |
| 1549 | {"colname": colname, "colwidth": colwidth} |
| 1550 | ) |
| 1551 | |
| 1552 | tgs_align.append(tg_align) |
| 1553 | |
| 1554 | # create unified range along the table groups |
| 1555 | tgs_range = self._create_tg_range(tgs_align) |
| 1556 | |
| 1557 | # if the structure is broken, return an empty table |
| 1558 | if not tgs_range: |
| 1559 | dl_table = TableData(num_rows=0, num_cols=0, table_cells=[]) |
| 1560 | return dl_table |
| 1561 | |
| 1562 | ncols_max = self._get_max_ncols(tgs_range) |
| 1563 | |
| 1564 | # extract table data |
| 1565 | table_data: list[TableCell] = [] |
| 1566 | i_row_global = 0 |
| 1567 | is_row_empty: bool = True |
| 1568 | tg_secs = table.find_all("tgroup") |
| 1569 | if tg_secs: |
| 1570 | for itg, tg_sec in enumerate(tg_secs): |
| 1571 | tg_range = tgs_range[itg] |
| 1572 | row_secs = tg_sec.find_all(["row", "tr"]) |
| 1573 | |
| 1574 | if row_secs: |
| 1575 | for row_sec in row_secs: |
| 1576 | entry_secs = row_sec.find_all(["entry", "td"]) |
| 1577 | is_header: bool = row_sec.parent.name in ["thead"] |
| 1578 | |
| 1579 | ncols = 0 |
| 1580 | local_row: list[TableCell] = [] |
| 1581 | is_row_empty = True |
| 1582 | if entry_secs: |
| 1583 | wrong_nbr_cols = False |
no test coverage detected