File tree 1 file changed +2
-9
lines changed
1 file changed +2
-9
lines changed Original file line number Diff line number Diff line change @@ -20,11 +20,7 @@ You can encrypt a PDF by using a password:
20
20
from pypdf import PdfReader, PdfWriter
21
21
22
22
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)
28
24
29
25
# Add a password to the new PDF
30
26
writer.encrypt(" my-secret-password" , algorithm = " AES-256" )
@@ -47,14 +43,11 @@ You can decrypt a PDF using the appropriate password:
47
43
from pypdf import PdfReader, PdfWriter
48
44
49
45
reader = PdfReader(" encrypted-pdf.pdf" )
50
- writer = PdfWriter()
51
46
52
47
if reader.is_encrypted:
53
48
reader.decrypt(" my-secret-password" )
54
49
55
- # Add all pages to the writer
56
- for page in reader.pages:
57
- writer.add_page(page)
50
+ writer = PdfWriter(clone_from = reader)
58
51
59
52
# Save the new PDF to a file
60
53
with open (" decrypted-pdf.pdf" , " wb" ) as f:
You can’t perform that action at this time.
0 commit comments