Files
docling-core/examples/rich_table_cells.ipynb
c73904e68e style: replace black, isort, flake8 and autoflake with ruff (#456)
* Added ruff to dev dependencies

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Added ruff settings to pyproject.toml as in docling

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Cleanup uf pyproject.toml

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Copied settings for ruff pre-commit hooks from docling

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Excluded test/data/** from ruff formatting / linting

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* ruff format

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Added some ignore statements to pyproject.toml such that ruff check raises fewer issues

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* ruff check --fix

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Ignored some more rules

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Fixed the rest of the errors that would only concern 1 - 3 files

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Added another ignore related to df for DataFrame names

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Modified CONTRIBUTING.md such that black / isort are replaced by ruff

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Added UP045 to ignore list such that Optional[...] does not raise

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Moved .flake8 configs to pyproject.toml

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Moved autoflake to be used with ruff

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Moved all .flake8 settings to pyproject.toml to be compatible with ruff (i.e. no separate [tool.flake8] section

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Removed flake8 from .pre-commit hooks

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Applied ruff format (again); formatted some files as the line-length = 120 equals now what was set for the .flake8 settings

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Set max-complexity to 30 (as was originally) in the pyproject.toml as one linting check would fail

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Adding PD901 to ignore list such that pre-commit hooks run fully again

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* Replaced dtype | None syntax by Optional[dtype] in remaining places

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>

* chore: fix 'test' ref in pyproject

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove typing List, Set, Tuple, Dict

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove UP015 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove UP034 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: normalize dashes in comments and docstrings

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove PD901 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove C403 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove C403, C413, C416 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* style: remove E203, F811 check from ignore list

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

---------

Signed-off-by: Florian Schwarb <florian.schwarb@gmail.com>
Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
Co-authored-by: Florian Schwarb <florian.schwarb@gmail.com>
Co-authored-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
2026-01-13 17:03:10 +01:00

14 KiB

In [1]:
from docling_core.types.doc import (
    DoclingDocument,
    TableData,
    TableCell,
    RichTableCell,
    DocItemLabel,
)

doc = DoclingDocument(name="")
doc.add_text(label=DocItemLabel.TITLE, text="Rich tables")

table_item = doc.add_table(
    data=TableData(
        num_rows=3,
        num_cols=2,
    ),
)

rich_item = doc.add_list_group(parent=table_item)
doc.add_list_item(parent=rich_item, text="list item 1")
doc.add_list_item(parent=rich_item, text="list item 2")

for i in range(table_item.data.num_rows):
    for j in range(table_item.data.num_cols):
        if i == 1 and j == 1:
            cell = RichTableCell(
                start_row_offset_idx=i,
                end_row_offset_idx=i + 1,
                start_col_offset_idx=j,
                end_col_offset_idx=j + 1,
                ref=rich_item.get_ref(),
            )
        else:
            cell = TableCell(
                start_row_offset_idx=i,
                end_row_offset_idx=i + 1,
                start_col_offset_idx=j,
                end_col_offset_idx=j + 1,
                text=f"cell {i},{j}",
            )
        doc.add_table_cell(table_item=table_item, cell=cell)

Doc-level exports

In [2]:
print(doc.export_to_markdown())
# Rich tables

| cell 0,0   | cell 0,1                    |
|------------|-----------------------------|
| cell 1,0   | - list item 1 - list item 2 |
| cell 2,0   | cell 2,1                    |
In [3]:
print(doc.export_to_html()[-300:])
em;
    }
</style>
</head>
<body>
<div class='page'>
<h1>Rich tables</h1>
<table><tbody><tr><td>cell 0,0</td><td>cell 0,1</td></tr><tr><td>cell 1,0</td><td><ul>
<li>list item 1</li>
<li>list item 2</li>
</ul></td></tr><tr><td>cell 2,0</td><td>cell 2,1</td></tr></tbody></table>
</div>
</body>
</html>
In [4]:
print(doc.export_to_doctags())
<doctag><title>Rich tables</title>
<otsl><fcel>cell 0,0<fcel>cell 0,1<nl><fcel>cell 1,0<fcel><unordered_list><list_item>list item 1</list_item>
<list_item>list item 2</list_item>
</unordered_list><nl><fcel>cell 2,0<fcel>cell 2,1<nl></otsl>
</doctag>
In [5]:
print(doc._export_to_indented_text())
item-0 at level 0: unspecified: group _root_
  item-1 at level 1: title: Rich tables
  item-2 at level 1: table with [3x2]
    item-3 at level 2: list: group group
      item-4 at level 3: list_item: list item 1
      item-5 at level 3: list_item: list item 2

Item-level exports

With document reference

In [6]:
print(doc.tables[0].export_to_markdown(doc=doc))
| cell 0,0   | cell 0,1                    |
|------------|-----------------------------|
| cell 1,0   | - list item 1 - list item 2 |
| cell 2,0   | cell 2,1                    |
In [7]:
print(doc.tables[0].export_to_html(doc=doc))
<table><tbody><tr><td>cell 0,0</td><td>cell 0,1</td></tr><tr><td>cell 1,0</td><td><ul>
<li>list item 1</li>
<li>list item 2</li>
</ul></td></tr><tr><td>cell 2,0</td><td>cell 2,1</td></tr></tbody></table>
In [8]:
print(doc.tables[0].export_to_doctags(doc=doc))
<otsl><fcel>cell 0,0<fcel>cell 0,1<nl><fcel>cell 1,0<fcel><unordered_list><list_item>list item 1</list_item>
<list_item>list item 2</list_item>
</unordered_list><nl><fcel>cell 2,0<fcel>cell 2,1<nl></otsl>
In [9]:
print(doc.tables[0].export_to_otsl(doc=doc))
<fcel>cell 0,0<fcel>cell 0,1<nl><fcel>cell 1,0<fcel><unordered_list><list_item>list item 1</list_item>
<list_item>list item 2</list_item>
</unordered_list><nl><fcel>cell 2,0<fcel>cell 2,1<nl>
In [10]:
display(doc.tables[0].export_to_dataframe(doc=doc))
0 1
0 cell 0,0 cell 0,1
1 cell 1,0 - list item 1\n- list item 2
2 cell 2,0 cell 2,1

Without document reference

In [11]:
print(doc.tables[0].export_to_markdown())
Usage of TableItem.export_to_markdown() without `doc` argument is deprecated.
| cell 0,0   | cell 0,1           |
|------------|--------------------|
| cell 1,0   | <!-- rich cell --> |
| cell 2,0   | cell 2,1           |
In [12]:
print(doc.tables[0].export_to_html())
Usage of TableItem.export_to_html() without `doc` argument is deprecated.
In [13]:
display(doc.tables[0].export_to_dataframe())
Usage of TableItem.export_to_dataframe() without `doc` argument is deprecated.
0 1
0 cell 0,0 cell 0,1
1 cell 1,0 <!-- rich cell -->
2 cell 2,0 cell 2,1

Row operations

In [14]:
doc.tables[0].data.insert_row(
    row_index=1,
    after=False,
    row=["foo", "bar"],
)
print(doc.export_to_markdown())
# Rich tables

| cell 0,0   | cell 0,1                    |
|------------|-----------------------------|
| foo        | bar                         |
| cell 1,0   | - list item 1 - list item 2 |
| cell 2,0   | cell 2,1                    |
In [15]:
backup_doc = doc.model_copy(deep=True)
In [16]:
_ = doc.tables[0].data.remove_row(row_index=2)
When table contains rich cells, `doc` argument must be provided, otherwise rich cell content will be left dangling.
In [17]:
print(doc.export_to_markdown())
# Rich tables

| cell 0,0   | cell 0,1   |
|------------|------------|
| foo        | bar        |
| cell 2,0   | cell 2,1   |

- list item 1
- list item 2
In [18]:
backup_doc.tables[0].data.remove_row(row_index=2, doc=backup_doc)

print(backup_doc.export_to_markdown())
# Rich tables

| cell 0,0   | cell 0,1   |
|------------|------------|
| foo        | bar        |
| cell 2,0   | cell 2,1   |
In [ ]: