docling icon indicating copy to clipboard operation
docling copied to clipboard

error when extracting simple table from official federal document

Open andrea83ny opened this issue 1 year ago • 5 comments

Bug

I'm trying to extract tables from this document that contains US elections data: https://www.fec.gov/resources/cms-content/documents/2024presgeresults.pdf for some reasons it merges the rows with STATE UT VT together, causing the output to be wrong. this is not a scanned document.

Steps to reproduce

simply extract

Docling version

accelerate==1.12.0 annotated-types==0.7.0 antlr4-python3-runtime==4.9.3 attrs==25.4.0 beautifulsoup4==4.14.2 certifi==2025.11.12 charset-normalizer==3.4.4 click==8.3.1 colorama==0.4.6 colorlog==6.10.1 dill==0.4.0 docling==2.63.0 docling-core==2.52.0 docling-ibm-models==3.10.2 docling-parse==4.7.1 et_xmlfile==2.0.0 Faker==38.2.0 filelock==3.20.0 filetype==1.2.0 fsspec==2025.10.0 huggingface-hub==0.36.0 idna==3.11 Jinja2==3.1.6 jsonlines==4.0.0 jsonref==1.1.0 jsonschema==4.25.1 jsonschema-specifications==2025.9.1 latex2mathml==3.78.1 lxml==6.0.2 markdown-it-py==4.0.0 marko==2.2.1 MarkupSafe==3.0.3 mdurl==0.1.2 mpire==2.10.2 mpmath==1.3.0 multiprocess==0.70.18 networkx==3.6 numpy==2.2.6 omegaconf==2.3.0 opencv-python==4.12.0.88 openpyxl==3.1.5 packaging==25.0 pandas==2.3.3 pillow==11.3.0 pluggy==1.6.0 polyfactory==3.1.0 psutil==7.1.3 pyclipper==1.3.0.post6 pydantic==2.12.4 pydantic-settings==2.12.0 pydantic_core==2.41.5 Pygments==2.19.2 pylatexenc==2.10 pypdfium2==4.30.0 python-dateutil==2.9.0.post0 python-docx==1.2.0 python-dotenv==1.2.1 python-pptx==1.0.2 pytz==2025.2 pywin32==311 PyYAML==6.0.3 rapidocr==3.4.2 referencing==0.37.0 regex==2025.11.3 requests==2.32.5 rich==14.2.0 rpds-py==0.29.0 rtree==1.4.1 safetensors==0.7.0 scipy==1.16.3 semchunk==2.2.2 setuptools==80.9.0 shapely==2.1.2 shellingham==1.5.4 six==1.17.0 soupsieve==2.8 sympy==1.14.0 tabulate==0.9.0 tokenizers==0.22.1 torch==2.9.1 torchvision==0.24.1 tqdm==4.67.1 transformers==4.57.3 tree-sitter==0.25.2 tree-sitter-c==0.24.1 tree-sitter-java==0.23.5 tree-sitter-javascript==0.25.0 tree-sitter-python==0.25.0 tree-sitter-typescript==0.23.2 typer==0.19.2 typing-inspection==0.4.2 typing_extensions==4.15.0 tzdata==2025.2 urllib3==2.5.0 xlsxwriter==3.2.9

Python version

3.13

andrea83ny avatar Nov 26 '25 15:11 andrea83ny

Hi @andrea83ny! I'm Dosu and I’m helping the docling team.

This is a known issue with table extraction in Docling, especially for PDFs where rows are tightly spaced or the internal structure is ambiguous. The merging of STATE, UT, and VT rows likely results from how Docling's table extraction model detects and maps row boundaries using bounding boxes and cell-matching heuristics. This can cause multiple rows to be merged if the model can't confidently separate them based on the PDF's layout [source].

You can try the following steps to improve row separation:

  • Set the table extraction options to use the most accurate model and disable cell matching, which sometimes helps avoid merging:

    table_structure_options = {"mode": "accurate", "do_cell_matching": False}
    

    If you're using the CLI, check if you can pass these as pipeline options, or set them in your script if using the API [source].

  • Enable table visualization/debugging to inspect how Docling detects rows and cells. This can help you see if the model is merging bounding boxes for those rows. Use the --debug-visualize-layout flag or the equivalent API option [source].

  • Before exporting to Markdown or another format, inspect the intermediate DoclingDocument (e.g., export to JSON or print the element tree) to check if the table structure is correct. If the rows are already merged at this stage, the issue is with extraction, not serialization [source].

  • Try extracting tables directly to Pandas DataFrames or JSON for more granular inspection and possible post-processing [source].

If the issue persists, it may be due to the specific way the PDF encodes its tables. In that case, you might get better results with external tools like Camelot or Tabula, or by manually post-processing the extracted data [source].

Let me know if you want example code for any of these steps!

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Nov 26 '25 15:11 dosubot[bot]

thank you @Dosu for the prompt answer! I'm quite new to docling - would it be possible for you to share a code snippet that does capture the table in the very first page properly? much appreciated

andrea83ny avatar Nov 26 '25 16:11 andrea83ny

Here's a Python code snippet that uses Docling to extract tables from just the first page of your PDF, with options set to improve row separation (avoiding merged rows like STATE, UT, VT). This example disables cell matching and uses the most accurate table extraction mode. It also shows how to export the result to a Pandas DataFrame for inspection.

from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.pipeline_options import PdfPipelineOptions, TableFormerMode
import pandas as pd

# Configure pipeline options for best table extraction quality
pipeline_options = PdfPipelineOptions(do_table_structure=True)
pipeline_options.table_structure_options.do_cell_matching = False  # Avoids merging rows/columns
pipeline_options.table_structure_options.mode = TableFormerMode.ACCURATE  # Most accurate model

# Create the document converter with these options
doc_converter = DocumentConverter(
    format_options={
        InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
    }
)

# Path or URL to your PDF
pdf_path = "https://www.fec.gov/resources/cms-content/documents/2024presgeresults.pdf"

# Convert only the first page (page_range is zero-based, so (0, 1) means just page 1)
conv_res = doc_converter.convert(pdf_path, page_range=(0, 1))

# Export tables to Pandas DataFrames for inspection
for i, table in enumerate(conv_res.document.tables):
    df = table.export_to_dataframe(doc=conv_res.document)
    print(f"Table {i} (first page):")
    print(df)

Key options:

  • do_cell_matching = False tells Docling to use its own table structure prediction, which often avoids row merging issues in tricky PDFs.
  • mode = TableFormerMode.ACCURATE uses the most precise table extraction model.
  • page_range=(0, 1) ensures only the first page is processed.

If you still see merged rows, try toggling do_cell_matching to True or use Docling's debug visualization to inspect detected table boundaries. For most digital PDFs, no extra preprocessing is needed, but for scanned/image-based PDFs, image quality and DPI can matter a lot.

References:

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Nov 26 '25 16:11 dosubot[bot]

thanks! it's still doing errors. It's adding an empty row between state NV and NJ. that row should have been state = NH and the values in row NV are not correct unfortunately

andrea83ny avatar Nov 26 '25 16:11 andrea83ny

Classification labels added: priority:medium, pdf.

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

github-actions[bot] avatar Nov 16 '26 15:11 github-actions[bot]