#5140·PyMuPDF

Page.get_label() raises IndexError when the page precedes the first label definition

Author: AhmedKishkiCreated Sep 25, 2026Updated Sep 28, 2026
Labelsfix developedhas test

Page.get_label() raises IndexError: list index out of range for a page that precedes the first entry of the document's /PageLabels number tree.

Such a document is legal — /PageLabels need not cover page 0 — and the same call already returns '' when a document has no /PageLabels at all. So "no label applies to this page" is an answer this API already expresses; it just cannot be reached when the tree exists but starts later.

In 1.28.2 the selection is pymupdf/utils.py:965; the same expression is on master in src/utils.py and src_classic/utils.py:

python
item = [x for x in labels if x[0] <= pgNo][-1]

For a page before the first rule the comprehension is empty and [-1] raises. The expression has no way to represent the case that exists.

How to reproduce

python
import pymupdf

doc = pymupdf.open()
doc.new_page()
doc.new_page()
doc.set_page_labels([{"startpage": 1, "prefix": "", "firstpagenum": 1, "style": "D"}])
print(doc.get_page_labels())   # [{'startpage': 1, 'prefix': '', 'firstpagenum': 1, 'style': 'D'}]
print(doc[1].get_label())      # '1'
print(doc[0].get_label())      # IndexError: list index out of range

Actual result

Traceback (most recent call last):
  File "pymupdf/__init__.py", line 11975, in get_label
    return utils.get_label_pno(page.number, labels)
  File "pymupdf/utils.py", line 965, in get_label_pno
    item = [x for x in labels if x[0] <= pgNo][-1]
IndexError: list index out of range

Expected result

'', as for a document that has no labels at all — or whatever you prefer for a page outside the defined range, but not an exception.

Notes

  • Found in the wild on a published journal PDF whose page labels begin on its second page. It aborted a whole corpus ingestion for the library's consumer: one such file made an entire collection un-ingestable, which is a much larger impact than a missing page label.
  • Possibly related to #3510 (closed as fixed in a next release), which was a wrong label on the first page rather than a crash. The current filtered selection answers that case correctly and leaves this one unrepresentable.

Versions

  • PyMuPDF 1.28.2
  • Python 3.12
  • Linux