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.
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
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
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
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
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.
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.- Print
prov[0].charspanfor every text item on page 1. - Find the item with the largest
bbox.tand check it is the top of a page. - Guard the page number lookup so it prints a dash when
provis empty, then run it on all four handbook files.
This is what real progress feels like.