Skip to content

Commit 3f21f92

Browse files
authored
DOC: Encryption/decryption: Clone document instead of copying all pages (#2546)
1 parent ed1d438 commit 3f21f92

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

docs/user/encryption-decryption.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ You can encrypt a PDF by using a password:
2020
from pypdf import PdfReader, PdfWriter
2121

2222
reader = PdfReader("example.pdf")
23-
writer = PdfWriter()
24-
25-
# Add all pages to the writer
26-
for page in reader.pages:
27-
writer.add_page(page)
23+
writer = PdfWriter(clone_from=reader)
2824

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

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

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

55-
# Add all pages to the writer
56-
for page in reader.pages:
57-
writer.add_page(page)
50+
writer = PdfWriter(clone_from=reader)
5851

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

0 commit comments

Comments
 (0)