如何用Python设置PDF文档权限(含编辑、打印权限及防修改)
Got it, let's break down how to lock down your PDF's permissions beyond just the open password using Python. You mentioned you're using PyPDF2, which does support granular permission controls—you just need to leverage the right parameters in the encrypt() method.
First, a Quick Primer on PDF Passwords
PDFs have two types of passwords, and they serve different purposes:
- User Password: What you've already set—this lets someone open the document.
- Owner Password: This is the key to controlling permissions. Even if someone has the user password, they can't modify restrictions unless they have the owner password. You must set this to enforce editing/printing limits.
Setting Restricted Permissions (Code Example)
Here's a full example that takes a PDF, sets a user password (for opening), an owner password (for permissions), and restricts editing while allowing only low-resolution printing:
from PyPDF2 import PdfWriter, PdfReader # Load your input PDF reader = PdfReader("your_input.pdf") writer = PdfWriter() # Copy all pages from the input to the writer for page in reader.pages: writer.add_page(page) # Define which permissions to allow (all others are blocked) # We'll only allow low-resolution printing here allowed_permissions = PdfWriter.ALLOW_PRINTING # Encrypt the PDF with both passwords and apply permissions writer.encrypt( user_pwd="user_open_pass123", # Password to open the PDF owner_pwd="owner_control_pass456", # Password to change permissions permissions=allowed_permissions, use_128bit=True # Use stronger 128-bit encryption (recommended) ) # Save the restricted PDF with open("restricted_output.pdf", "wb") as output_file: writer.write(output_file)
Common Permission Scenarios
PyPDF2 uses bitmask constants to define allowed actions. Here are some useful combinations:
- No editing, only high-quality printing allowed:
allowed_permissions = PdfWriter.ALLOW_PRINTING_HIGH - Allow screen readers and low-quality printing, block all edits:
allowed_permissions = PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_SCREENREADERS - Completely lock down (only allow viewing, no printing/editing):
allowed_permissions = 0 # No permissions granted besides opening - Allow form filling but no content edits:
allowed_permissions = PdfWriter.ALLOW_FILL_FORM
Key PyPDF2 Permission Constants to Know
Here's a quick reference for the most relevant constants:
ALLOW_PRINTING: Low-resolution printingALLOW_PRINTING_HIGH: High-resolution printingALLOW_MODIFY_CONTENTS: Edit document contentALLOW_COPY: Copy text/imagesALLOW_MODIFY_ANNOTATIONS: Edit comments/annotationsALLOW_FILL_FORM: Fill in interactive formsALLOW_SCREENREADERS: Access for screen reader toolsALLOW_ASSEMBLY: Insert/delete/reorder pages
Important Notes
- Owner Password is Mandatory: You can't enforce permissions without setting an owner password. If you only set a user password, all editing/printing actions will be allowed by default.
- Encryption Strength: Always set
use_128bit=Truefor stronger encryption (the alternative is 40-bit, which is easily bypassed). - Reader Compatibility: These permissions work with Adobe Acrobat and most modern PDF readers, as they follow the PDF 1.4 security standard.
- Bypass Limitations: Keep in mind that no PDF permission system is 100% unbreakable, but this will block casual users and comply with standard security workflows.
内容的提问来源于stack exchange,提问作者Applex007

