Markdown, and the arguments that change it
export_to_markdown has been printing whole documents since lesson 2. It takes about twenty arguments, and four of them are worth knowing.
Markdown is the export you feed to a model, paste into a ticket, or show a person. It is also the export that throws things away, and knowing which things is the point of this lesson.
Only some labels
The labels from lesson 6 can be used as a filter, and the filter runs before anything is written.
from docling_core.types.doc import DocItemLabel
wanted = {DocItemLabel.TITLE, DocItemLabel.SECTION_HEADER}
print(document.export_to_markdown(labels=wanted))An outline of the document, with the heading markers still on it. Passing {DocItemLabel.TABLE} instead gives you every table and nothing else, which is a quick way to see what a long report actually contains.
Only part of it
print(document.export_to_markdown(from_element=1, to_element=3))The numbers are positions in the walk from lesson 7, not line numbers, so element 1 is the first heading. Handy for showing the top of a long document without converting it twice.
Narrower tables
print(document.export_to_markdown(compact_tables=True))The default pads every cell so the columns line up when a person reads the raw text. compact_tables drops the padding, which on a wide table saves a surprising number of tokens before it reaches a model.
An argument that does nothing
The signature still has a delim argument for the string between blocks. It has been deprecated, and passing it changes nothing at all.
plain = document.export_to_markdown()
narrow = document.export_to_markdown(delim="\n")
print(plain == narrow)A warning goes to the error stream and the export comes back unchanged. This is worth meeting once, because it is the shape of a whole class of bug: an argument that is accepted, ignored, and leaves you looking for the mistake somewhere else. strict_text on the same method behaves the same way, and lesson 21 shows where that one leaks into the command line.
What Markdown loses
Here is a table where one cell covers two rows.
<h1>Couriers</h1>
<table>
<tr><th>Courier</th><th>Zone</th><th>Days</th></tr>
<tr><td rowspan="2">Bluedart</td><td>Metro</td><td>2</td></tr>
<tr><td>Rural</td><td>5</td></tr>
</table>from docling.document_converter import DocumentConverter
couriers = DocumentConverter().convert("couriers.html").document
print(couriers.export_to_markdown())Markdown has no syntax for a cell that covers two rows, so the serializer writes the spanning cell's text into both positions. Nothing is missing and nothing says a span was ever there: Bluedart now looks like two separate entries. Lesson 11 shows the export that keeps it.
- Export with
labels={DocItemLabel.TABLE}onrefunds.html. - Pass
image_placeholder="[figure]"and convert a document containing a picture. - Print the span of
couriers.tables[0].data.grid[2][0]and compare it with the Markdown you just saw.
Every expert started right here.