IRONSOFTWAREHOME
USING IRONPDF FOR PYTHON

Python PdfWriter(程式碼範例教程)

Curtis Chau
Curtis Chau
Updated: 2026年4月21日

IronPDF 是一個純 Python 的 PDF 文件物件程式庫,適用於希望在應用程式中編寫 PDF 文件或操作 PDF 文件的 Python 開發者。 IronPDF 因其簡單性和多功能性而脫穎而出,使其成為需要自動化 PDF 建立或將 PDF 生成整合到軟體系統中的理想選擇。

本指南將探討如何使用 IronPDF 這個純 Python 的 PDF 程式庫來建立 PDF 文件或生成 PDF 頁面屬性以及讀取 PDF 文件。 將包含範例和實用程式碼片段,使您能夠實際了解如何在 Python 專案中使用 IronPDF for Python 的 PdfWriter 來編寫 PDF 文件並建立新的 PDF 頁面。

設置 IronPDF

安裝

要開始使用 IronPDF,您需要通過 Python Package Index 來安裝它。 在終端運行以下命令:

> pip install ironpdf

編寫 PDF 文件和操作 PDF 文件

建立新 PDF

IronPDF 簡化了新建 PDF 文件和處理現有 PDF 的過程。 它提供了簡單的接口來生成文件,不論是簡單的單頁 PDF 還是帶有各種元素(如使用者密碼)的複雜文件。 這項功能對於報告生成、建立發票等任務至關重要。

from ironpdf import ChromePdfRenderer, License, Logger

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Basic HTML content for the PDF
html = """
<html>
   <head>
      <title>IronPDF for Python!</title>
      <link rel='stylesheet' href='assets/style.css'>
   </head>
   <body>
      <h1>It's IronPDF World!!</h1>
      <a href="https://ironpdf.com/python/"><img src='assets/logo.png' /></a>
   </body>
</html>
"""

# Create a PDF renderer
renderer = ChromePdfRenderer()
# Render the HTML content as a PDF
pdf = renderer.RenderHtmlAsPdf(html)
# Save the rendered PDF to a file
pdf.SaveAs("New PDF File.pdf")
Python

Python PdfWriter (程式碼範例教學),圖 1:輸出文件 輸出文件

合併 PDF 文件

IronPDF 簡化了將多個 PDF 文件合併成一個的任務。 此功能對於匯總各種報告、組裝掃描文件或組織原屬於一起的資訊特別有用。 例如,當您需要從多個來源建立綜合報告或有一系列文件需要作為單一文件呈現時,可能需要合併 PDF 文件。

from ironpdf import PdfDocument, License, Logger

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Load existing PDF documents
pdfOne = PdfDocument("Report First.pdf")
pdfTwo = PdfDocument("Report Second.pdf")
# Merge the PDFs into a single document
merged = PdfDocument.Merge(pdfOne, pdfTwo)
# Save the merged PDF
merged.SaveAs("Merged.pdf")
Python

將現有的 PDF 文件合併成新 PDF 文件的能力在像是資料科學等領域中也是非常有用的,合併的 PDF 文件可以作為訓練 AI 模組的資料集。 IronPDF 可以輕鬆處理這項任務,保持每個原始文件頁面的完整性和格式,從而產生無縫且連貫的輸出 PDF 文件。

Python PdfWriter (程式碼範例教學),圖 2:合併的 PDF 輸出 合併的 PDF 輸出

拆分單一 PDF

相對地,IronPDF 在將現有的 PDF 文件拆分成多個新文件方面也表現出色。 當您需要從一個大容量 PDF 文件中提取指定部分或將文件劃分為更小、更易於管理的部分時,此功能非常實用。

from ironpdf import PdfDocument, License, Logger

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Load the PDF document
pdf = PdfDocument("Report.pdf")
# Extract the first page
page1doc = pdf.CopyPage(0)
# Save the extracted page as a new PDF
page1doc.SaveAs("Split1.pdf")
Python

例如,您可能想要從一份大型報告中隔離特定的 PDF 頁面,或從一本書的不同章節建立個別文件。 IronPDF 讓您可以選擇所需的多個頁面轉換成新的 PDF 文件,確保您可以根據需要操作和管理您的 PDF 內容。

Python PdfWriter (程式碼範例教學),圖 3:拆分的 PDF 輸出 拆分的 PDF 輸出

實施安全功能

當處理敏感或機密資訊時,保護您的 PDF 文件成為首要任務。 IronPDF 通過提供強大的安全功能來滿足這一需求,包括使用者密碼保護和加密功能。 這可確保您的 PDF 文件保持安全,僅授權使用者可以存取。

from ironpdf import PdfDocument, License, Logger

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Load the PDF document
pdf = PdfDocument("Report.pdf")
# Adjust security settings to make the PDF read-only and set permissions
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
# Set the document encryption passwords
pdf.SecuritySettings.OwnerPassword = "top-secret"  # password to edit the PDF
pdf.SecuritySettings.UserPassword = "sharable"  # password to open the PDF
# Save the secured PDF
pdf.SaveAs("secured.pdf")
Python

通過實施使用者密碼,您可以控管誰可以查看或編輯您的 PDF 文件。 加密選項增加了一層額外的安全性,保護您的資料免受未授權存取,使 IronPDF 成為管理 PDF 格式敏感資訊的可靠選擇。

從 PDF 中提取文字

IronPDF 的另一個關鍵功能是其可以從 PDF 文件中提取文字的能力。 此功能對於資料檢索、內容分析,甚至是為了將現有 PDF 的文字內容重新用於新文件特別有用。

from ironpdf import PdfDocument, License, Logger

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Load the PDF document
pdf = PdfDocument("Report.pdf")
# Extract all text from the PDF document
allText = pdf.ExtractAllText()
# Extract text from a specific page in the document
specificPage = pdf.ExtractTextFromPage(3)
Python

無論您是在提取資料進行分析、在大型文件中尋找特定資訊,或將內容從 PDF 轉換為文字文件以進行進一步處理,IronPDF 都使其變得簡單和高效。 程式庫確保提取的文字保持其原始格式和結構,使其立即可用於您的具體需求。

管理文件資訊

PDF 的高效管理超越了其內容。 IronPDF 提供了有效管理文件元資料和屬性的功能,如作者名稱、文件標題、建立日期等。 此功能對於在需要記錄文件來源和元資料的環境中,組織和編目您的 PDF 文件是至關重要的。

from ironpdf import PdfDocument, License, Logger
from datetime import datetime

# Set the IronPDF license key
License.LicenseKey = "Your-License-Key"
# Enable logging for debugging purposes
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All

# Load an existing PDF or create a new one
pdf = PdfDocument("Report.pdf")
# Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = datetime.now()
# Save the PDF with updated metadata
pdf.SaveAs("MetaData Updated.pdf")
Python

例如,在學術或企業環境中,跟蹤文件的建立日期和作者身份對於記錄保持和文件檢索目的可能是必需的。 IronPDF 使管理此資訊變得簡單,提供了一種簡化的方式在您的 Python 應用中處理和更新文件資訊。

結論

授權

本教程涵蓋了在 Python 中使用 IronPDF 進行 PDF 操作的基本知識。 從建立新 PDF 文件到合併現有文件,並增加安全功能,IronPDF 是任何 Python 開發人員的多功能工具。

IronPDF for Python還提供以下功能:

IronPDF for Python 提供免費試用以供使用者探索其功能。 試用結束後,如需繼續使用,授權起價為 $999。 這一價格允許開發人員在其項目中充分利用 IronPDF 的全部功能範圍。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

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天試用金鑰。
無需信用卡或帳戶建立
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起