One call for all three
The three parsers from lesson 1 become one call that does not care which format it was given.
Installing
pip install doclingThat 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
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.
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>.
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.
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.
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.- Convert
shipping.mdand compare the output with the file, character by character. One difference is deliberate. - Point
convertat a file that does not exist and read the error. - Rename
orders.csvtoorders.txtand convert it again.
You understood something today that you didn't yesterday.