Pulling out just the outline
Lesson 3 iterated every item in reading order. Sometimes you don't want every item, you want the document's outline: what are the section headings, in order, with body text stripped out. Because doc.texts items carry a .label, this is a filter, not a parser:
from docling_core.types.doc import DocItemLabel
headings = [ t for t in doc.texts if t.label in (DocItemLabel.TITLE, DocItemLabel.SECTION_HEADER)]DocItemLabel is the enum backing every .label value seen so far (title, section_header, text, and more used in later lessons). Importing it directly, instead of comparing against the raw string "section_header", keeps this code correct even if the underlying string values ever change across docling versions.
export_to_text()
export_to_markdown() produces Markdown syntax, # for headings, | for tables. export_to_text() produces the same reading-order content without any of that, just plain paragraphs and simple - bullets. Reach for it when the destination can't handle Markdown at all, an older full-text search index, a plain log file, a system that would choke on stray # characters.
Checkpoint
- Filtering by
.label: pulling the outline out of aDoclingDocumentis a list comprehension overdoc.texts, not a regex or a Markdown parser. DocItemLabel: the enum backing every.label, safer to import and compare against than raw strings.export_to_text(): the same reading-order content asexport_to_markdown(), without Markdown syntax, for destinations that can't handle#and|.
If anything here still feels unclear, ask before moving to Lesson 6.