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,
)
| 1023 | |
| 1024 | |
| 1025 | def get_toc( |
| 1026 | doc: Document, |
| 1027 | simple: bool = True, |
| 1028 | ) -> list: |
| 1029 | """Create a table of contents. |
| 1030 | |
| 1031 | Args: |
| 1032 | 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. |
| 1033 | """ |
| 1034 | |
| 1035 | def recurse(olItem, liste, lvl): |
| 1036 | """Recursively follow the outline item chain and record item information in a list.""" |
| 1037 | while olItem: |
| 1038 | if olItem.title: |
| 1039 | title = olItem.title |
| 1040 | else: |
| 1041 | title = " " |
| 1042 | |
| 1043 | if not olItem.is_external: |
| 1044 | if olItem.uri: |
| 1045 | if olItem.page == -1: |
| 1046 | resolve = doc.resolve_link(olItem.uri) |
| 1047 | page = resolve[0] + 1 |
| 1048 | else: |
| 1049 | page = olItem.page + 1 |
| 1050 | else: |
| 1051 | page = -1 |
| 1052 | else: |
| 1053 | page = -1 |
| 1054 | |
| 1055 | if not simple: |
| 1056 | link = getLinkDict(olItem) |
| 1057 | liste.append([lvl, title, page, link]) |
| 1058 | else: |
| 1059 | liste.append([lvl, title, page]) |
| 1060 | |
| 1061 | if olItem.down: |
| 1062 | liste = recurse(olItem.down, liste, lvl + 1) |
| 1063 | olItem = olItem.next |
| 1064 | return liste |
| 1065 | |
| 1066 | # ensure document is open |
| 1067 | if doc.is_closed: |
| 1068 | raise ValueError("document closed") |
| 1069 | doc.init_doc() |
| 1070 | olItem = doc.outline |
| 1071 | if not olItem: |
| 1072 | return [] |
| 1073 | lvl = 1 |
| 1074 | liste = [] |
| 1075 | toc = recurse(olItem, liste, lvl) |
| 1076 | if doc.is_pdf and simple is False: |
| 1077 | doc._extend_toc_items(toc) |
| 1078 | return toc |
| 1079 | |
| 1080 | |
| 1081 | def del_toc_item( |
nothing calls this directly
no test coverage detected
searching dependent graphs…