MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / set_page_labels

Method set_page_labels

src/__init__.py:7083–7125  ·  view source on GitHub ↗

Add / replace page label definitions in PDF document. Args: doc: PDF document (resp. 'self'). labels: list of label dictionaries like: {'startpage': int, 'prefix': str, 'style': str, 'firstpagenum': int}, as returned by get_page_labels().

(doc, labels)

Source from the content-addressed store, hash-verified

7081 raise ValueError("bad PageMode value")
7082
7083 def set_page_labels(doc, labels):
7084 """Add / replace page label definitions in PDF document.
7085
7086 Args:
7087 doc: PDF document (resp. 'self').
7088 labels: list of label dictionaries like:
7089 {'startpage': int, 'prefix': str, 'style': str, 'firstpagenum': int},
7090 as returned by get_page_labels().
7091 """
7092 # William Chapman, 2021-01-06
7093
7094 def create_label_str(label):
7095 """Convert Python label dict to corresponding PDF rule string.
7096
7097 Args:
7098 label: (dict) build rule for the label.
7099 Returns:
7100 PDF label rule string wrapped in "<<", ">>".
7101 """
7102 s = f"{label['startpage']}<<"
7103 if label.get("prefix", "") != "":
7104 s += f"/P({label['prefix']})"
7105 if label.get("style", "") != "":
7106 s += f"/S/{label['style']}"
7107 if label.get("firstpagenum", 1) > 1:
7108 s += f"/St {label['firstpagenum']}"
7109 s += ">>"
7110 return s
7111
7112 def create_nums(labels):
7113 """Return concatenated string of all labels rules.
7114
7115 Args:
7116 labels: (list) dictionaries as created by function 'rule_dict'.
7117 Returns:
7118 PDF compatible string for page label definitions, ready to be
7119 enclosed in PDF array 'Nums[...]'.
7120 """
7121 labels.sort(key=lambda x: x["startpage"])
7122 s = "".join([create_label_str(label) for label in labels])
7123 return s
7124
7125 doc._set_page_labels(create_nums(labels))
7126
7127 def set_toc(
7128 doc: 'Document',

Callers 2

test_setlabelsFunction · 0.80
test_labels_styleAFunction · 0.80

Calls 2

create_numsFunction · 0.85
_set_page_labelsMethod · 0.80

Tested by 2

test_setlabelsFunction · 0.64
test_labels_styleAFunction · 0.64