Python PdfWriter(程式碼範例教程)
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")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")
輸出文件
合併 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")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")將現有的 PDF 文件合併成新 PDF 文件的能力在像是資料科學等領域中也是非常有用的,合併的 PDF 文件可以作為訓練 AI 模組的資料集。 IronPDF 可以輕鬆處理這項任務,保持每個原始文件頁面的完整性和格式,從而產生無縫且連貫的輸出 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")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")例如,您可能想要從一份大型報告中隔離特定的 PDF 頁面,或從一本書的不同章節建立個別文件。 IronPDF 讓您可以選擇所需的多個頁面轉換成新的 PDF 文件,確保您可以根據需要操作和管理您的 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")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")通過實施使用者密碼,您可以控管誰可以查看或編輯您的 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)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)無論您是在提取資料進行分析、在大型文件中尋找特定資訊,或將內容從 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")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")例如,在學術或企業環境中,跟蹤文件的建立日期和作者身份對於記錄保持和文件檢索目的可能是必需的。 IronPDF 使管理此資訊變得簡單,提供了一種簡化的方式在您的 Python 應用中處理和更新文件資訊。
結論
授權
本教程涵蓋了在 Python 中使用 IronPDF 進行 PDF 操作的基本知識。 從建立新 PDF 文件到合併現有文件,並增加安全功能,IronPDF 是任何 Python 開發人員的多功能工具。
IronPDF for Python還提供以下功能:
- 使用 HTML 或 URL 從頭開始建立新 PDF 文件
- 編輯現有 PDF 文件
- 旋轉PDF 頁面
- 提取文字、元資料和圖片從 PDF 文件
- 使用密碼和限制保護 PDF 文件
- Split and merge PDFs
IronPDF for Python 提供免費試用以供使用者探索其功能。 試用結束後,如需繼續使用,授權起價為 $999。 這一價格允許開發人員在其項目中充分利用 IronPDF 的全部功能範圍。
常見問題
我如何在Python中建立PDF文件?
您可以使用IronPDF的CreatePdf方法來生成新的PDF文件。此方法允許您使用Python從頭建立自定義PDF文件。
安裝IronPDF for Python的步驟有哪些?
要在Python中安裝IronPDF,您可以通過執行命令:pip install ironpdf來使用Python Package Index。
如何使用Python將多個PDF合併為一個?
IronPDF提供合併多個PDF文件的功能。您可以使用MergePdfFiles方法來將多個PDF合併為一個單獨的文件。
我可以使用IronPDF將PDF拆分為單獨的頁面嗎?
是的,IronPDF提供SplitPdf函式,允許您將PDF分割成每頁或部分,並為每個部分建立單獨的文件。
IronPDF支持哪些PDF安全功能?
IronPDF支持多種安全功能,包括密碼保護和加密,以確保您的PDF文件安全且僅供授權使用者存取。
我如何在Python中從PDF文件中提取文字?
使用IronPDF,您可以輕鬆通過ExtractText方法從PDF文件中提取文字,這對於資料檢索和分析非常有用。
IronPDF提供哪些主要的PDF操作功能?
IronPDF允許您建立、合併和拆分PDF,應用安全措施,提取文字,並管理文件元資料,如作者姓名和建立日期。
IronPDF是否有免費試用版?我該如何存取?
是的,IronPDF提供免費試用。您可以在試用期間探索其功能,並在試用結束後購買授權以繼續使用。
IronPDF在Python專案中的一些實際應用例有哪些?
IronPDF非常適合於生成報告、建立發票、保護文件,以及在各種Python專案中管理PDF元資料。
我如何使用IronPDF管理PDF元資料?
IronPDF使您能夠管理PDF元資料,包括作者姓名、文件標題和建立日期,這對於文件組織和編目至關重要。









