下面的代码非常适合我最初要完成的工作。但是,如果我尝试手动向该PDF添加新的第一页和书签,则现有的书签目标会向后移动一页并且不会链接到它们最初合并的位置。
有什么建议吗?有没有办法在合并期间锁定书签目的地?
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'))