【问题标题】:Trying to add bookmark to pdf using PyPDF2尝试使用 PyPDF2 将书签添加到 pdf
【发布时间】:2021-03-05 06:30:46
【问题描述】:

我试图附加一个包含名称和页码的书签,但我无法使用书签保存文件。 这是代码:

import PyPDF2 as pdf
import os

pth = r'D:\Haymovich\pythonScriptTry\pdf\old'
save_pth = r'D:\Haymovich\pythonScriptTry\pdf\new\MainPDF.pdf'


def pdf_marge(inp):
    mar = pdf.PdfFileMerger()
    for num, eachPDF in enumerate(os.listdir(inp)):
        eachPDF2 = os.path.join(pth, eachPDF)
        eachPDF3 = eachPDF.replace('.pdf', '')
        bookMark = mar.addBookmark(title=eachPDF3, pagenum=num)
        mar.append(fileobj=eachPDF2, bookmark=bookMark)
    return mar.write(save_pth)

print(pdf_marge(pth))

这是书签:

{'/Title': 'NESS01-2020', '/Page': 0, '/Type': '/FitH', '/Top': 826}

{'/Title': 'NESS01-2021', '/Page': 1, '/Type': '/FitH', '/Top': 826}

{'/Title': 'NESS02-2020', '/Page': 2, '/Type': '/FitH', '/Top': 826}

{'/Title': 'NESS03-2020', '/Page': 3, '/Type': '/FitH', '/Top': 826}

谁能帮我保存带有书签的pdf?

【问题讨论】:

    标签: python bookmarks pypdf2


    【解决方案1】:

    你没有打开文件

    改变

    return mar.write(save_pth)
    

    with open(save_pth, "wb") as new_pdf:
        mar.write(new_pdf)
    

    【讨论】:

    • 不,对不起,它做同样的事情,我尝试将书签添加到文件的开头:(
    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多