Skip to content

Getting a Line Number From an Annotation #1197

Discussion options

You must be logged in to vote

Ah, that's better.
First, you need to get the bboxes of all lines on the page.
Second, take the bbox (annot.rect) of your annotation and look in which line's bbox it is contained. Some snippet like this:

lines = []
for b in page.get_text("dict")["blocks"]:
    if b["type"] != 0: continue
    for l in b["lines"]:
        lines.append(l)
# now sort the lines - they may be in arbitrary order!
lines.sort(key=lambda l: (l["bbox"][3], l["bbox"][0]))  # ascending vertical, then horizontal
# for some annot do this:
for i, line in enumerate(lines):
    if annot.rect in fitz.Rect(line["bbox"]):
        print(annot, "contained in line %i" % i)
        found = True
        break
if not found:
    print(

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Nehemaya
Comment options

Comment options

You must be logged in to vote
1 reply
@Nehemaya
Comment options

Answer selected by Nehemaya
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants