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

Which page, and where on it

A citation that says it is in the returns policy somewhere is not a citation. Every piece of a converted PDF carries the page it was on and the box it filled.

Example
item = document.texts[1]
print(item.text)
print("page", item.prov[0].page_no)

prov is a list because one piece can span a page break, in which case it has an entry per page. For a paragraph that fits on one page there is exactly one entry.

The box

Example
box = document.texts[1].prov[0].bbox
print(round(box.l), round(box.b), round(box.r), round(box.t))
print(box.coord_origin)

Left, bottom, right and top, in the same points as the page size. The origin is the bottom left corner, so t is a larger number than b and the top of the page has the biggest numbers of all. Getting that backwards is the usual first mistake when drawing these boxes on a page image.

Page by page

Example
for item in document.texts:
    print(item.prov[0].page_no, item.label, "|", item.text[:35])

Four pieces on page one and four on page two. That column is what turns a search hit into page 2 of the returns policy, and it is what the citation in lesson 29 prints.

Exporting one page

Example
print(document.export_to_markdown(page_no=2))

The page_no argument from lesson 11 works on every export, and it is the quickest way to check that a page came out the way you expected without reading the whole document.

Only PDFs have this

Example
from docling.document_converter import DocumentConverter

html = DocumentConverter().convert("refunds.html").document
print(html.texts[0].prov)

An empty list. HTML has no pages and no coordinates, so there is nothing to record. Code that reads prov[0] without checking works on your PDF and fails on the first web page it meets.

The character span too. prov[0].charspan gives the start and end positions of this piece inside the page's own text, which is how a highlight can be drawn over the original file rather than over the converted text.
Try it yourself
  • Print prov[0].charspan for every text item on page 1.
  • Find the item with the largest bbox.t and check it is the top of a page.
  • Guard the page number lookup so it prints a dash when prov is empty, then run it on all four handbook files.

This is what real progress feels like.