Doclingdocling 2.127.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
26 small wins to finish your pathNext lesson

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.

Example
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

Example
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.

Example
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.

The full list is longer than you need. Along with the six here there are labels for captions, footnotes, page headers and footers, formulas, code, checkboxes and form fields. Print DocItemLabel from docling_core.types.doc when you want to see them.
Try it yourself
  • Print the label of every item in refunds.html and note which one the table gets.
  • Ask for .level on a paragraph and read the error you get.
  • Change # Shipping to ## Shipping and see which piece becomes the title.

Little by little, you're building something great.