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

时间:2019-03-14 23:55:24

标签: python python-3.x pypdf2

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

是否可以删除注释?或任何可以帮助我摆脱空白的建议。

1 个答案:

答案 0 :(得分:1)

方法from azure.storage.blob import BlockBlobService from io import BytesIO from shutil import copyfileobj with BytesIO() as input_blob: with BytesIO() as output_blob: block_blob_service = BlockBlobService(account_name='my_account_name', account_key='my_account_key') # Download as a stream block_blob_service.get_blob_to_stream('mycontainer', 'myinputfilename', input_blob) # Do whatever you want to do - here I am just copying the input stream to the output stream copyfileobj(input_blob, output_blob) ... # Create the a new blob block_blob_service.create_blob_from_stream('mycontainer', 'myoutputfilename', output_blob) # Or update the same blob block_blob_service.create_blob_from_stream('mycontainer', 'myinputfilename', output_blob) 删除链接和注释。因此,如果可以同时丢失两者,则可以在第一个代码块中添加PdfFileWriter.removeLinks(),这很正常。