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

How to remove annotations in pdf in Python 3

我最初的目标是去除 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'])

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

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