Skip to content

Obtaining end page number for each bookmark in ToC #764

Answered by JorjMcKie
andrei-volkau asked this question in Q&A
Discussion options

You must be logged in to vote

I think I understand.
The one thing that makes a "reliable" algorithm a bit complex is that the items in TOC need not point to page numbers in an ascending (or at least not descending) sequence, iaw item[i][2] <= item[i + 1][2] cannot be assumed to be true - although probable.
But how about this snippet:

>>> toc = doc.getToC()
>>> item = [i for i in toc if i[1].startswith("1.1 ")][0]  # find item whose end page is desired
>>> level=item[0]  # its level
>>> pno=item[2]  # its page number
>>> toclist = [i for i in toc if i[0] <= level and i[2] >= pno]  # list of bookmark candidates
>>> toclist.sort(key=lambda i: i[2])  # sort by page number to be sure
>>> idx = toclist.index(item)  # our it…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@andrei-volkau
Comment options

Answer selected by andrei-volkau
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #764 on December 16, 2020 13:29.