docling icon indicating copy to clipboard operation
docling copied to clipboard

Table cells come back empty with do_cell_matching=False (PDF and image)

Open sibisrinivasb opened this issue 6 days ago • 1 comments

Bug

I turned off cell matching (do_cell_matching=False), which the docs suggest when columns get merged. The table structure still comes out right, but a lot of the cells come back empty. On an image the whole table is empty.

From what I can see, without matching each cell gets its text from page._backend.get_text_in_rect(bbox):

  • With the default docling-parse backend, that only returns a text line if more than half of the line is inside the box. docling-parse often stores a whole row as one line (on the page below, OTSL 0.965 0.934 0.955 0.88 is one line across five cells), so none of those cells get any text.
  • With an image, get_text_in_rect always returns "". The OCR text is already in the table tokens, it just isn't used for the cells.

PyPdfium is fine because it cuts the text per character.

Steps to reproduce

Using the test page from the repo, tests/data/pdf/sources/2305.03393v1-pg9.pdf. Run from a docling checkout:

repro.py
import tempfile
from pathlib import Path

import pypdfium2 as pdfium

from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions, RapidOcrOptions
from docling.document_converter import (
    DocumentConverter,
    ImageFormatOption,
    PdfFormatOption,
)

PDF = Path("tests/data/pdf/sources/2305.03393v1-pg9.pdf")


def cells_with_text(source: Path, kind: str, matching: bool) -> str:
    options = PdfPipelineOptions(do_ocr=kind == "png", ocr_options=RapidOcrOptions())
    options.table_structure_options.do_cell_matching = matching
    if kind == "png":
        fmt = {InputFormat.IMAGE: ImageFormatOption(pipeline_options=options)}
    elif kind == "pypdfium":
        fmt = {
            InputFormat.PDF: PdfFormatOption(
                pipeline_options=options, backend=PyPdfiumDocumentBackend
            )
        }
    else:
        fmt = {InputFormat.PDF: PdfFormatOption(pipeline_options=options)}
    doc = DocumentConverter(format_options=fmt).convert(source).document
    cells = doc.tables[0].data.table_cells
    return f"{sum(1 for c in cells if c.text.strip())} / {len(cells)}"


def main() -> None:
    png = Path(tempfile.mkdtemp()) / "page.png"
    pdf = pdfium.PdfDocument(PDF)
    pdf[0].render(scale=2.0).to_pil().convert("RGB").save(png)
    pdf.close()
    print("input                     matching on  matching off")
    for name, source, kind in (
        ("PDF, default backend", PDF, "pdf"),
        ("PDF, PyPdfium backend", PDF, "pypdfium"),
        ("PNG of the page, OCR", png, "png"),
    ):
        on = cells_with_text(source, kind, True)
        off = cells_with_text(source, kind, False)
        print(f"{name:25s} {on:12s} {off}")


if __name__ == "__main__":
    main()

Cells that have text:

input                     matching on  matching off
PDF, default backend      41 / 41      17 / 41
PDF, PyPdfium backend     41 / 41      41 / 41
PNG of the page, OCR      41 / 41      0 / 41

I'd expect the cells to keep their text with matching off too. I have a fix for this and can open a PR.

Docling version

main (01f7db4, 2.154.0), also on 2.150.1. docling-core 2.108.0, docling-parse 7.28.0, docling-ibm-models 4.0.4

Python version

3.12, Windows 11, CPU

sibisrinivasb avatar Nov 29 '26 21:11 sibisrinivasb

Classification labels added: priority:medium, pdf, image.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 29 '26 21:11 github-actions[bot]