3

The code below works great on what I'm trying to initially accomplish. However, if I try to manually add a new first page and bookmark to that PDF the existing bookmark destinations move back one page and are not linked to where they originally merged.

Any suggestions? Is there a way to lock bookmark destinations during the merge?

import os
from PyPDF2 import PdfFileMerger
...
for pdf in PDFfiles: # Merge each PDF into a single PDF and add Bookmarks
    name = os.path.splitext(os.path.basename(pdf))[0] # Split filename from .pdf extension
    dp = name.index("$") + 1 # Find position of $ sign
    bookmarkname = name[dp:].replace("_", " ") # set bookmark name & replace underscores with spaces    
    merger.append(open(os.path.join(cdir,pdf), 'rb'), bookmark=bookmarkname) # Add next PDF to the end of the previous & add bookmark name
    merger.setPageMode("/UseOutlines") # Show Bookmarks on open

merger.write(open(os.path.join(odir,newname), 'wb'))
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
theurlin
  • 181
  • 1
  • 1
  • 8

0 Answers0