Skip to content

Instantly share code, notes, and snippets.

@readpan
Created August 29, 2023 05:14
Show Gist options
  • Save readpan/0df3886ff7c57e0be98ef906a7d5470d to your computer and use it in GitHub Desktop.
Save readpan/0df3886ff7c57e0be98ef906a7d5470d to your computer and use it in GitHub Desktop.
Remove all annotations of PDF file
import PyPDF2
pdf_path = '/your/path/to/pdf'
pdf_save_path = '/your/path/to/save/pdf'
# This attempts to remove annotations
with open(pdf_path, 'rb') as pdf_obj:
pdf = PyPDF2.PdfFileReader(pdf_obj)
pdf_out = PyPDF2.PdfFileWriter()
# remove all annotations
for page in pdf.pages:
page = page.getObject()
pdf_out.addPage(page)
pdf_out.removeLinks()
with open(pdf_save_path, 'wb') as out:
pdf_out.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment