如何在Python 3中删除pdf中的注释

问题描述 投票:2回答:1

我最初的目标是删除PDF页面上的大量白边。

然后我发现这个目的可以通过使用下面的代码缩放页面来实现,但注释不会缩放。

import PyPDF2

# This works fine
with open('old.pdf', 'rb') as pdf_obj:
    pdf = PyPDF2.PdfFileReader(pdf_obj)
    out = PyPDF2.PdfFileWriter()
    for page in pdf.pages:
        page.scale(2, 2)
        out.addPage(page)
    with open('new.pdf', 'wb') as f: 
        out.write(f)

# This attempts to remove annotations
with open('old.pdf', 'rb') as pdf_obj:
    pdf = PyPDF2.PdfFileReader(pdf_obj)
    page = pdf.pages[2]
    print(page['/Annots'], '\n\n\n\n')
    page.Annots = []
    print(page['/Annots'])

有没有办法删除注释?或任何可以帮助我摆脱白边的建议。

python python-3.x pypdf2
1个回答
1
投票

方法PdfFileWriter.removeLinks()删除链接和注释。所以,如果你可以丢失两个,你可以在你的第一个代码块中添加out.removeLinks(),那个工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.