can docling extract heading, start page, endpage, bbox, transcript
{ "title": "Practical Applications and Case Studies", "startPage": 48, "endPage": 55, "bbox": [72.0, 698.5, 456.8, 723.7], "transcript": "Real-world applications of machine learning are demonstrated through comprehensive case studies. This final chapter includes projects in sentiment analysis, recommendation systems, fraud detection, and predictive maintenance. Each case study follows a complete machine learning pipeline from data collection and preprocessing to model deployment and monitoring. Best practices for production deployment, model versioning, and continuous integration are discussed. The chapter concludes with emerging trends in machine learning and future directions in the field. " } ]
sample response body that im expecting when i pass a file to that endpoint
so give me the code to achieve it
Yes, Docling can extract headings, start page, end page, bounding boxes, and transcripts from documents via the /v1/convert/file endpoint. The response includes a DoclingDocument JSON object containing all the necessary fields, but you'll need to post-process it to match your expected format.
Here's a Python code snippet that takes the DoclingDocument JSON response and extracts the required fields:
def extract_sections(docling_json):
results = []
# 'texts' contains headings, titles, and other text elements
for item in docling_json.get("texts", []):
# Section headers and titles
if item.get("label") in ["TITLE", "SECTION_HEADER"]:
title = item.get("text")
# Provenance contains bbox and page info
prov = item.get("prov", [])
if prov:
bbox = prov[0].get("bbox")
start_page = prov[0].get("page_no")
# Try to find end page (if available)
end_page = prov[-1].get("page_no")
else:
bbox = None
start_page = None
end_page = None
# Transcript: collect all child text items under this heading
transcript = ""
for child_ref in item.get("children", []):
child = child_ref.get("resolved") # You may need to resolve references
if child and child.get("label") == "TEXT":
transcript += child.get("text", "") + " "
results.append({
"title": title,
"startPage": start_page,
"endPage": end_page,
"bbox": [bbox.get("l"), bbox.get("t"), bbox.get("r"), bbox.get("b")] if bbox else None,
"transcript": transcript.strip()
})
return results
You can call this function with the json_content field from the API response. If you want a single section, just pick the one you need from the results.
All the required fields are present in the DoclingDocument structure, so you can adapt this code as needed for your workflow. For more details on the DoclingDocument schema, see docling_core/types/doc/document.py.
To reply, just mention @dosu.
Docs are dead. Just use Dosu.
it is taking docling_json right so give me a code to convert a pdf or any document to docling_json
You can convert a PDF (or other supported document) to docling_json using either the Docling Python library or the Docling API.
Python library approach:
from docling.document_converter import DocumentConverter
# For most formats (PDF, DOCX, PPTX, etc.), this is enough:
result = DocumentConverter().convert("yourfile.pdf")
doc = result.document
doc.save_to_json("yourfile.docling.json")
This will save the structured docling_json to a file. You can also use doc.export_to_dict() if you want the JSON as a Python dict instead of saving to disk.
API approach:
You can POST your file to the /v1/convert/file endpoint. Example using Python requests:
import requests
with open("yourfile.pdf", "rb") as f:
files = {"files": f}
response = requests.post("http://your-docling-server/v1/convert/file", files=files)
docling_json = response.json()["document"]["json_content"]
No special authentication or config is required unless your server is set up with auth.
Once you have the docling_json, you can use the extraction code I shared earlier to get headings, page numbers, bbox, and transcript fields from the JSON structure.
For more advanced options (like enabling OCR or customizing pipelines), see the DocumentConverter and pipeline options in the docling library docs or codebase. Supported formats include PDF, DOCX, PPTX, Markdown, HTML, and more.
Reference | Example
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
is there any other way other than save_to_json because it is not working give me complete code from converting to docling document to extract all sections bbox and all
You don't need save_to_json—use export_to_dict() to get the docling_json in memory. Here’s a complete example that converts a PDF (or any supported file) to a DoclingDocument, then extracts all sections/headings with bbox, startPage, endPage, title, and transcript, matching your expected output:
from docling.document_converter import DocumentConverter
def extract_sections(doc):
results = []
# Build a lookup for resolving references
texts_by_ref = {item.self_ref: item for item in doc.texts}
for item in doc.texts:
if getattr(item, "label", None) in ["TITLE", "SECTION_HEADER"]:
title = getattr(item, "text", "")
prov = getattr(item, "prov", [])
bbox = prov[0].bbox if prov else None
start_page = prov[0].page_no if prov else None
end_page = prov[-1].page_no if prov else None
# Gather transcript from children (TEXT items)
transcript = ""
for child_ref in getattr(item, "children", []):
child = child_ref.resolve(doc)
if getattr(child, "label", None) == "TEXT":
transcript += getattr(child, "text", "") + " "
results.append({
"title": title,
"startPage": start_page,
"endPage": end_page,
"bbox": [bbox.l, bbox.t, bbox.r, bbox.b] if bbox else None,
"transcript": transcript.strip()
})
return results
# Convert your document (PDF, DOCX, etc.)
result = DocumentConverter().convert("yourfile.pdf")
doc = result.document # This is a DoclingDocument object
# Extract sections as a list of dicts
sections = extract_sections(doc)
# If you want the raw docling_json as a dict (no file I/O):
docling_json = doc.export_to_dict()
This approach works fully in memory and does not require save_to_json. You can adapt the extraction logic as needed for your workflow. For more details, see the DoclingDocument API and DocumentConverter.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
Docling's output already contains most of these fields. Each section header item has its page and bounding box in item.prov[0], and the transcript is the text of the items between that header and the next one, so the end page is the page of the last of those items. If you also need heading levels, Docling's heading-hierarchy stage (HeadingHierarchyOptions(enabled=True), off by default) assigns them from the bookmarks, then numbering, then font style.
A minimal approach is to walk doc.iterate_items() in order, start a new section at each section_header item, and append text items to the current section until the next header.
I built Section Anything (open source, Apache-2.0), which returns the title, level, page range and text per section directly as JSON, in case that is useful. It does not provide bounding boxes, however, so Docling remains the right source for the bbox field.