What Docling decided each piece is
Every piece carries a label, and the label is the part no plain text parser gives you. It is also the part that is a decision, which means it can be wrong.
Lesson 4 printed labels without saying what they are. A label is the kind of thing Docling decided a piece is: a title, a heading, a paragraph, an item in a list. There are thirty of them in this version and a normal document uses five or six.
from docling.document_converter import DocumentConverter
document = DocumentConverter().convert("shipping.md").document
for item in document.texts:
print(item.label, "|", item.text[:35])The first hash became a title and the two double hashes became section_header. Nothing in the Markdown says title; that is Docling reading the first top-level heading of a document as its title.
How deep a heading sits
for item in document.texts:
if item.label == "section_header":
print(item.level, item.text)level is on section headers and nowhere else, so reaching for it on a paragraph is an error. Both headings here are level 1, because both are ## and the # above them became the title rather than a level.
Filtering by label
A label is a plain string, so the usual filtering works and reads well.
wanted = {"title", "section_header"}
outline = [t.text for t in document.texts if t.label in wanted]
print(outline)That is a document outline in three lines, from a Markdown file, an HTML page or a PDF, without a single format-specific rule. Lesson 10 uses the same set to cut an export down to its headings.
A label is a decision
For Markdown and HTML the label comes from the markup, so it is reliable. For a PDF there is no markup: a layout model looks at the page and decides, and it can call a bold line a heading when it was a heavy paragraph. Lesson 17 is about knowing when that happened.
DocItemLabel from docling_core.types.doc when you want to see them.- Print the label of every item in
refunds.htmland note which one the table gets. - Ask for
.levelon a paragraph and read the error you get. - Change
# Shippingto## Shippingand see which piece becomes the title.
Little by little, you're building something great.