Welcome, guest | Sign In | My Account | Store | Cart

PyMuPDF now supports a simple interface for maintaining table of contents / bookmarks and metadata for PDF documents. By manipulating ordinary, elementary lists and dictionaries you can add, delete, modify a PDF's metadata and table of contents.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import fitz                                      # = PyMuPDF
 >>> doc = fitz.open("test.pdf")                      # open PDF
 >>> toc = doc.getToC()                               # get current table of contents
 >>> for t in toc: print(t)                           # show what we have so far
 ...
 [1, 'The PyMuPDF Documentation', 1]                  # = [hierarchy-level, title, page-number]
 [2, 'Introduction', 1]
 [3, 'Note on the Name fitz', 1]
 [3, 'License', 1]
 >>> toc[1][1] += " modified by setToC"               # modify anything
 >>> doc.setToC(toc)                                  # replace outline tree
 0
 >>> for t in doc.getToC(): print(t)                  # demonstrate it worked
 ...
 [1, 'The PyMuPDF Documentation', 1]
 [2, 'Introduction modified by setToC', 1]            # <<< this has changed!
 [3, 'Note on the Name fitz', 1]
 [3, 'License', 1]
 >>>
 >>> # similar for metadata:
 >>> doc.setMetadata({"author":"Jorj. X. McKie", "creator": ...})
 0
 >>> 
 >>> # when done, incrementally save to the file works in sub-second time
 >>> doc.save(doc.name, incremental=True)
 0
 >>>

2 comments

Jorj X. McKie (author) 7 years, 10 months ago  # | flag

download PyMuPDF from GitHub

Jorj X. McKie (author) 7 years, 10 months ago  # | flag

In PyMuPDF's repository on GitHub, also find a fully featured GUI bookmark editor based on wxPython.