IRONSOFTWAREHOME
PDF 加密與解密
from ironpdf import *

# Open an Encrypted File, alternatively create a new PDF from Html
pdf = PdfDocument.FromFile("encrypted.pdf", "password")
 
# Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = Now()
 
# Edit file security settings
# The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserFormData = False
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
 
# Change or set the document encryption password
pdf.Password = "my-password"
pdf.SaveAs("secured.pdf")
pip install ironpdf
PDF 加密與解密

PDF 加密與解密

IronPDF for Python可以打開受密碼保護的PDF文件、修改其中繼資料和安全設置,並以新密碼重新保存它們——在幾行程式碼中實現完整的加密和解密工作流程。

入門指南

將文件路徑和現有密碼傳遞給PdfDocument.FromFile以打開加密的PDF。 根據需要修改安全設置和中繼資料,然後將新密碼分配給SaveAs保存。

理解程式碼

  • PdfDocument.FromFile(path, password): 使用提供的密碼解密以打開加密的PDF文件。 如果密碼不正確,引發異常。
  • MetaData.Author / Keywords / ModifiedDate: 解密後可讀取或更新的中繼資料欄位。
  • SecuritySettings.RemovePasswordsAndEncryption(): 從記憶體文件中刪除當前加密,允許重新配置安全設置。
  • SecuritySettings.MakePdfDocumentReadOnly(key): 使用新的內部密鑰重新應用唯讀保護。
  • SecuritySettings.AllowUser*: 細粒度的使用者許可標誌——設置為False以限制特定操作。
  • pdf.Password = "my-password": 設置開啟保存的PDF所需的新密碼。
  • SaveAs("secured.pdf"): 保存應用所有新設置的重新加密文件。

不重新加密的解密

要生成受密碼保護的PDF的未加密副本, 請調用Password的情況下保存。

學習如何使用IronPDF for Python保護PDF文件!

準備開始了嗎?

版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
PDF的Python模塊下載
使用pip安裝

版本: 2026.9

  1. 下載並安裝Python 3.7+。
  2. 如果尚未安裝,請從 pypi.org 安裝 pip。
  3. 在終端執行上述命令。
Python PDF模塊
下載模塊

版本: 2026.9

手動安裝到您的專案中

  1. 下載套件
  2. 從終端運行此命令
    pip install ironpdf-2026.9-py37-none-win_amd64.whi

從$999起的授權