[Python] Accessing Docling features from within spaCy Layout in Python

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    Currently I'm using spacy-layout as part of a pipeline to OCR documents and analyse documents. However, I also need to access other features of Docling such as counting the number of images in each document and exporting them. Ideally I'd be able to do things like ignore logo images in Spacy Layout headers and footers while doing further processing on images in the layout body.

    I quickly realised that span.label_ == "picture" doesn't work because spans are naturally text only:

    # Count figures
    picture_spans = [
    span for span in doc.spans["layout"] if span.label_ == "picture"
    ]
    figure_count = len(picture_spans)


    Instead I can use something like the following code based on Docling's figure export example: https://docling-project.github.io/docling/examples/export_figures/

    def count_document_images(file_path):
    # Set up pipeline options
    pipeline_options = PdfPipelineOptions()
    pipeline_options.images_scale = 2.0 # Similar to IMAGE_RESOLUTION_SCALE in example
    pipeline_options.generate_picture_images = True

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

    try:
    # Convert the document
    conv_res = doc_converter.convert(Path(file_path))

    # Count pictures
    picture_count = 0
    for element, _level in conv_res.document.iterate_items():
    if isinstance(element, PictureItem):
    picture_count += 1

    return picture_count

    except Exception as e:
    print(f"Error counting images in {file_path}: {str(e)}")
    return 0


    However, this means I'm currently processing the document with Docling twice, first to process the images and then to get the spacy-layout. Is there a way to access the wider features of the Docling representation from within spacy-layout? Otherwise is their a recommended way to pre-process the document with Docling and then pass it to spacy-layout so I get a consistent representation of the document without reprocessing it?

    Continue reading...

Compartilhe esta Página