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

One call for all three

The three parsers from lesson 1 become one call that does not care which format it was given.

Installing

bash
pip install docling

That is the whole install. It pulls in the document backends for every format in this course and the machine learning parts needed for PDFs; those models are downloaded later, the first time a PDF is actually converted.

The same three lines, three times

Example
from docling.document_converter import DocumentConverter

converter = DocumentConverter()
document = converter.convert("shipping.md").document
print(document.export_to_markdown())

convert takes a path and works out the format from it. export_to_markdown writes the document back out as text. For a Markdown file that is close to a round trip, which makes it a fair thing to look at first.

Example
print(converter.convert("refunds.html").document.export_to_markdown())

Same call, HTML in, and the table came out as a Markdown table with its header row intact. Nobody wrote a rule about <th>.

Example
print(converter.convert("orders.csv").document.export_to_markdown())

A CSV file becomes a document containing one table. That is a slightly odd idea until you see why it is useful: the spreadsheet and the web page now hold the same kind of object, so one piece of code can walk both.

What the converter knows

A DocumentConverter holds a mapping from format to the code that reads it. Building one is cheap for the formats in this lesson. Reuse it rather than making a new one per file: for PDFs it is the object that holds the loaded models.

Example
from docling.datamodel.base_models import InputFormat

print(len(list(InputFormat)), "formats")
print([fmt.value for fmt in InputFormat][:8])

Thirty-three of them in this version, from docx and pptx through images and audio to email. Lesson 19 comes back to this list and to what happens when a file is not on it.

A URL works where a path does. convert("https://arxiv.org/pdf/2408.09869") downloads the file and converts it. Nothing in this course needs the network, so every example here uses a local path.
Try it yourself
  • Convert shipping.md and compare the output with the file, character by character. One difference is deliberate.
  • Point convert at a file that does not exist and read the error.
  • Rename orders.csv to orders.txt and convert it again.

You understood something today that you didn't yesterday.