使用 pikepdf 模块将轮廓从 PDF1 复制到 PDF2

问题描述 投票:0回答:0

我写了一个代码来重新排序 PDF1 的页面并将它们保存到 PDF2:

import pikepdf

def Main():
    with pikepdf.open("pdf1.pdf") as sourcePDF:
        sourcePages = sourcePDF.pages
        targetPDF = pikepdf.Pdf.new()
        targetPages = targetPDF.pages
        # Reorder the pages basing on the previously provided list
        for idx in [1, 7, 2, 3, 4, 5, 6, 0]:
            targetPages.append(sourcePages[idx])
        targetPDF.save("pdf2.pdf")
        
if __name__ == "__main__":
    Main()

有没有办法复制大纲(书签等),并将它们分配到所需的页面? 到目前为止,我尝试过这样的事情:

with sourcePDF.open_outline() as sourceOutline:
    with targetPDF.open_outline() as targetOutline:
        for outline in sourceOutline.root:
            targetOutline.root.append(sourcePDF.copy_foreign(outline))

但我不知道如何正确使用 copy_foreign 方法。

python pdf outline pikepdf
© www.soinside.com 2019 - 2024. All rights reserved.