MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / get_toc

Method get_toc

src/__init__.py:5197–5249  ·  view source on GitHub ↗

Create a table of contents. Args: simple: a bool to control output. Returns a list, where each entry consists of outline level, title, page number and link destination (if simple = False). For details see PyMuPDF's documentation.

(
            doc: 'Document',
            simple: bool = True,
            )

Source from the content-addressed store, hash-verified

5195 return sigflag
5196
5197 def get_toc(
5198 doc: 'Document',
5199 simple: bool = True,
5200 ) -> list:
5201 """Create a table of contents.
5202
5203 Args:
5204 simple: a bool to control output. Returns a list, where each entry consists of outline level, title, page number and link destination (if simple = False). For details see PyMuPDF's documentation.
5205 """
5206 def recurse(olItem, liste, lvl):
5207 """Recursively follow the outline item chain and record item information in a list."""
5208 while olItem and olItem.this.m_internal:
5209 if olItem.title:
5210 title = olItem.title
5211 else:
5212 title = " "
5213
5214 if not olItem.is_external:
5215 if olItem.uri:
5216 if olItem.page == -1:
5217 resolve = doc.resolve_link(olItem.uri)
5218 page = resolve[0] + 1
5219 else:
5220 page = olItem.page + 1
5221 else:
5222 page = -1
5223 else:
5224 page = -1
5225
5226 if not simple:
5227 link = utils.getLinkDict(olItem, doc)
5228 liste.append([lvl, title, page, link])
5229 else:
5230 liste.append([lvl, title, page])
5231
5232 if olItem.down:
5233 liste = recurse(olItem.down, liste, lvl + 1)
5234 olItem = olItem.next
5235 return liste
5236
5237 # ensure document is open
5238 if doc.is_closed:
5239 raise ValueError("document closed")
5240 doc.init_doc()
5241 olItem = doc.outline
5242 if not olItem:
5243 return []
5244 lvl = 1
5245 liste = []
5246 toc = recurse(olItem, liste, lvl)
5247 if doc.is_pdf and not simple:
5248 doc._extend_toc_items(toc)
5249 return toc
5250
5251 def get_xml_metadata(self):
5252 """Get document XML metadata."""

Callers 13

delete_pagesMethod · 0.95
test_2634Function · 0.80
test_deletionFunction · 0.80
test_simple_tocFunction · 0.80
test_full_tocFunction · 0.80
test_erase_tocFunction · 0.80
test_replace_tocFunction · 0.80
test_setcolorsFunction · 0.80
test_circularFunction · 0.80
test_2355Function · 0.80
test_2788Function · 0.80
test_toc_countFunction · 0.80

Calls 3

recurseFunction · 0.85
init_docMethod · 0.80
_extend_toc_itemsMethod · 0.80

Tested by 12

test_2634Function · 0.64
test_deletionFunction · 0.64
test_simple_tocFunction · 0.64
test_full_tocFunction · 0.64
test_erase_tocFunction · 0.64
test_replace_tocFunction · 0.64
test_setcolorsFunction · 0.64
test_circularFunction · 0.64
test_2355Function · 0.64
test_2788Function · 0.64
test_toc_countFunction · 0.64
test_3820Function · 0.64