docling icon indicating copy to clipboard operation
docling copied to clipboard

ODS: sheet page size is the span of the boxes, so tables not starting at A1 get clamped

Open dlaszlo opened this issue 3 weeks ago • 1 comments

Bug

For an ODS file, each sheet becomes a page whose size is computed by _find_page_size in docling/backend/opendocument_backend.py (L2653-L2675), called from the sheet loop (L2396). It returns the span of the boxes, (right - left, bottom - top), where left and top are the smallest box edges.

The boxes count cells from A1 = (0, 0). So whenever the content does not start at A1, the page is smaller than the content: a table in B2:C3 has the box l=1 t=1 r=3 b=3, but the page is 2 × 2.

The wrong page size becomes a wrong table box as soon as the document is validated again, for example on DoclingDocument.model_validate_json or when a serializer is built on the document. validate_document in docling_core/types/doc/document.py (L5837-L5845) calls _clamp_provenance_bboxes_to_pages (L340), which clamps every box to its page with a UserWarning. The table box becomes r=2 b=2: the table loses its last row and its last column. A cell range computed from the box then reads B2 instead of B2:C3.

The Excel backend computes the same page size correctly: its _find_page_size in docling/backend/msexcel_backend.py (L2852-L2878) takes the largest r and b, and its docstring explains why the span is wrong for cell coordinates.

Tables that start at A1 are not affected (the span then equals the extent).

Steps to reproduce

A sheet with a table in B2:C3 (needs odfdo):

from io import BytesIO

from docling.datamodel.base_models import DocumentStream
from docling.document_converter import DocumentConverter
from docling_core.types.doc import DoclingDocument, TableItem
from odfdo import Document, Table

ods = Document("spreadsheet")
table = Table("Sheet1")
table.set_value("B2", "a")
table.set_value("C2", "b")
table.set_value("B3", "1")
table.set_value("C3", "2")
ods.body.clear()
ods.body.append(table)
buffer = BytesIO()
ods.save(buffer)
buffer.seek(0)

doc = DocumentConverter().convert(DocumentStream(name="sheet.ods", stream=buffer)).document
item = next(i for i, _ in doc.iterate_items() if isinstance(i, TableItem))
print("page size:", doc.pages[1].size)
print("table bbox:", item.prov[0].bbox)

reloaded = DoclingDocument.model_validate_json(doc.model_dump_json())
item = next(i for i, _ in reloaded.iterate_items() if isinstance(i, TableItem))
print("table bbox after reload:", item.prov[0].bbox)

Output (docling 2.145.0, docling-core 2.105.0):

page size: width=2.0 height=2.0
table bbox: l=1.0 t=1.0 r=3.0 b=3.0
UserWarning: Provenance bbox coordinate r on page 1 is outside page bounds: value=3.0 > hi=2.0; clamping to 2.0
table bbox after reload: l=1.0 t=1.0 r=2.0 b=2.0

Expected: page size 3 × 3, and the table keeps r=3.0 b=3.0 after a reload.

Possible fix

Size the ODS page the way the Excel backend's _find_page_size does: the largest r and b across all items of the page, in every content layer (a hidden sheet's items are not in the body layer):

@staticmethod
def _find_page_size(doc: DoclingDocument, page_no: int) -> tuple[float, float]:
    """Return (width, height) for the page in cell-index units: the largest right and
    bottom edge of its items, since the boxes count cells from (0, 0)."""
    width: float = 0.0
    height: float = 0.0
    for item, _ in doc.iterate_items(
        traverse_pictures=True,
        page_no=page_no,
        included_content_layers=set(ContentLayer),
    ):
        if not isinstance(item, DocItem):
            continue
        for provenance in item.prov:
            if provenance.page_no != page_no:
                continue
            width = max(width, provenance.bbox.r)
            height = max(height, provenance.bbox.b)
    return (width, height)

A test could build a sheet whose table starts at B2 and check that its bbox is still r=3, b=3 after DoclingDocument.model_validate_json(doc.model_dump_json()).

Docling version

docling 2.145.0, docling-core 2.105.0; the code is the same in docling v2.150.0 and docling-core v2.106.0 (the line numbers above are from those tags)

Python version

3.14

dlaszlo avatar Nov 11 '26 17:11 dlaszlo

Hi! I would like to work on this issue. I inspected _find_page_size() in docling/backend/opendocument_backend.py and compared it against docling/backend/msexcel_backend.py: _find_page_size() currently computes the bounding-box span (right - left, bottom - top) and omits included_content_layers=set(ContentLayer) (as well as filtering by provenance.page_no), causing non-A1 tables and hidden sheets to produce undersized page bounds that get clamped on validation/reload. I plan to align OdsDocumentBackend._find_page_size() with the extent computation (max(r), max(b)) across all content layers and add regression tests in ests/test_backend_opendocument.py.

Nani-2007-code avatar Nov 11 '26 17:11 Nani-2007-code