{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "81ee4096", "metadata": {}, "outputs": [], "source": [ "from docling_core.types.doc import (\n", " DoclingDocument,\n", " TableData,\n", " TableCell,\n", " RichTableCell,\n", " DocItemLabel,\n", ")\n", "\n", "doc = DoclingDocument(name=\"\")\n", "doc.add_text(label=DocItemLabel.TITLE, text=\"Rich tables\")\n", "\n", "table_item = doc.add_table(\n", " data=TableData(\n", " num_rows=3,\n", " num_cols=2,\n", " ),\n", ")\n", "\n", "rich_item = doc.add_list_group(parent=table_item)\n", "doc.add_list_item(parent=rich_item, text=\"list item 1\")\n", "doc.add_list_item(parent=rich_item, text=\"list item 2\")\n", "\n", "for i in range(table_item.data.num_rows):\n", " for j in range(table_item.data.num_cols):\n", " if i == 1 and j == 1:\n", " cell = RichTableCell(\n", " start_row_offset_idx=i,\n", " end_row_offset_idx=i + 1,\n", " start_col_offset_idx=j,\n", " end_col_offset_idx=j + 1,\n", " ref=rich_item.get_ref(),\n", " )\n", " else:\n", " cell = TableCell(\n", " start_row_offset_idx=i,\n", " end_row_offset_idx=i + 1,\n", " start_col_offset_idx=j,\n", " end_col_offset_idx=j + 1,\n", " text=f\"cell {i},{j}\",\n", " )\n", " doc.add_table_cell(table_item=table_item, cell=cell)" ] }, { "cell_type": "markdown", "id": "7ef93338", "metadata": {}, "source": [ "## Doc-level exports" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba4d71d9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Rich tables\n", "\n", "| cell 0,0 | cell 0,1 |\n", "|------------|-----------------------------|\n", "| cell 1,0 | - list item 1 - list item 2 |\n", "| cell 2,0 | cell 2,1 |\n" ] } ], "source": [ "print(doc.export_to_markdown())" ] }, { "cell_type": "code", "execution_count": 3, "id": "8a528e15", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "em;\n", " }\n", "\n", "\n", "
\n", "| cell 0,0 | cell 0,1 |
| cell 1,0 |
|
| cell 2,0 | cell 2,1 |