Plain text, and HTML that keeps the spans
Two more exports, and the choice between them is really a choice about what you are going to do with the result.
Plain text is less plain than it sounds
print(document.export_to_text())The heading markers are gone, which is what plain means. The table is not gone: it is still a Markdown table, pipes and all, because there is no way to write a grid in plain text and Docling does not invent one.
So export_to_text is the right export when you want the words without the markup getting counted as words, and the wrong one if you were expecting the table to disappear.
HTML keeps everything Markdown dropped
from docling.document_converter import DocumentConverter
couriers = DocumentConverter().convert("couriers.html").document
print(couriers.tables[0].export_to_html(couriers))There is the rowspan that lesson 10's Markdown had to flatten. HTML has the syntax for it, so the serializer uses it. When a merged header cell matters to whatever reads your output, this is the export to use.
A whole page, not a fragment
page = document.export_to_html()
print(len(page.splitlines()), "lines")
print(page.splitlines()[0])export_to_html on the document writes a complete page with a stylesheet in it, ready to open in a browser. export_to_html on a single table, as above, writes just that table. Two methods with the same name at two levels, and the difference catches people out.
Choosing
| You want to | Use | Because |
|---|---|---|
| Feed a model | export_to_markdown | Compact, and models read it well |
| Count or search words | export_to_text | No heading markers in the way |
| Keep merged cells | export_to_html | The only text export with span syntax |
| Store and reload | save_as_json | The only one that loses nothing |
labels, from_element, to_element and page_no work on Markdown, text and HTML alike, so a rule you worked out on one applies to the others.- Export
refunds.htmlas text and count the pipe characters. - Open the exported HTML page in a browser.
- Call
export_to_html(page_no=2)on the PDF once you reach lesson 13.
Little by little, you're building something great.