Practice building single password-protected PDFs, batch unique password-encrypted documents from list data, and setting user vs owner permissions directly inside Google Colab.
View on GitHubWe'll start with a simple example of creating a single password-protected PDF and then move on to generating multiple PDFs with unique passwords for each file.
from pathlib import Path
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'single_protected.pdf'
pw = pdfencrypt.StandardEncryption('test')
c = canvas.Canvas(str(my_path), bottomup=0, encrypt=pw)
c.drawString(50, 200, "Hello World")
c.showPage()
c.save()
print(f"Single protected PDF created: {my_path}")
Let's break down the code step by step:
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw = pdfencrypt.StandardEncryption('test')
pathlib.Path:
my_path = Path('output/single_protected.pdf')
c = canvas.Canvas(str(my_path), bottomup=0, encrypt=pw)
c.drawString(50, 200, "Hello World")
c.showPage()
c.save()
from pathlib import Path
from datetime import datetime
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
l1 = [
(1, 'John Deo', 'Four', 75, 'female', '2010-02-25'),
(2, 'Max Ruin', 'Three', 85, 'male', '2010-12-12'),
(3, 'Arnold', 'Three', 55, 'male', '2011-03-31')
]
for rows in l1:
f = rows[0]
my_path = output_dir / f'my_pdf_{f}.pdf'
dt = datetime.strptime(rows[5], '%Y-%m-%d')
str_dt = datetime.strftime(dt, '%b')
str_name = rows[1][0:3]
str_pw = (str_name + str_dt).lower()
pw = pdfencrypt.StandardEncryption(str_pw)
c = canvas.Canvas(str(my_path), bottomup=0, encrypt=pw)
my_data = f"{rows[0]},{rows[1]},{rows[2]},{rows[3]},{rows[4]}"
c.drawString(50, 200, my_data)
c.showPage()
c.save()
print(f"Generated {my_path} with password '{str_pw}'")
| Filename | Generated Password |
|---|---|
my_pdf_1.pdf |
johfeb |
my_pdf_2.pdf |
maxdec |
my_pdf_3.pdf |
arnmar |
The unique password for each PDF file is generated by combining the first three characters of the name and the abbreviated month part of the date, both converted to lowercase.
For example, the name "John Deo" and date "2010-02-25" produce the password "johfeb".
With the PDF security handler, we can specify two types of passwords for a document:
from pathlib import Path
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'restricted_permissions.pdf'
pw = pdfencrypt.StandardEncryption(
userPassword='xyz',
ownerPassword='abc',
canPrint=False,
canModify=False,
canCopy=False,
canAnnotate=False
)
c = canvas.Canvas(str(my_path), bottomup=0, encrypt=pw)
c.drawString(50, 200, "This PDF is password and permission protected")
c.showPage()
c.save()
print(f"Restricted permission PDF generated: {my_path}")
Q: Can I encrypt existing PDF files without recreating them?
A: ReportLab creates PDFs from scratch. To encrypt existing PDF files, use libraries like pypdf or PyMuPDF.
Q: What is the difference between User and Owner passwords?
A: The User password allows viewing under restricted permissions. The Owner password unlocks all restrictions and enables permission changes.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.