Skip to content

Commit

Permalink
DOC: Encryption/decryption: Clone document instead of copying all pag…
Browse files Browse the repository at this point in the history
…es (#2546)
  • Loading branch information
redfast00 committed Mar 27, 2024
1 parent ed1d438 commit 3f21f92
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions docs/user/encryption-decryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ You can encrypt a PDF by using a password:
from pypdf import PdfReader, PdfWriter

reader = PdfReader("example.pdf")
writer = PdfWriter()

# Add all pages to the writer
for page in reader.pages:
writer.add_page(page)
writer = PdfWriter(clone_from=reader)

# Add a password to the new PDF
writer.encrypt("my-secret-password", algorithm="AES-256")
Expand All @@ -47,14 +43,11 @@ You can decrypt a PDF using the appropriate password:
from pypdf import PdfReader, PdfWriter

reader = PdfReader("encrypted-pdf.pdf")
writer = PdfWriter()

if reader.is_encrypted:
reader.decrypt("my-secret-password")

# Add all pages to the writer
for page in reader.pages:
writer.add_page(page)
writer = PdfWriter(clone_from=reader)

# Save the new PDF to a file
with open("decrypted-pdf.pdf", "wb") as f:
Expand Down

0 comments on commit 3f21f92

Please sign in to comment.