使用IRONPDF FOR PYTHON

Python PdfWriter(代碼範例教程)

已更新 2024年10月7日
分享:

介紹

IronPDF是一個純 Python 的 PDF 檔案物件庫,適合尋找編寫 PDF 文件或在其應用程式中操作 PDF 文件。 IronPDF以其簡便性和多功能性脫穎而出,成為需要自動化PDF創建或在軟體系統中整合PDF生成的任務的理想選擇。

本指南將探討如何使用 IronPDF,一個純 Python 的 PDF 庫,用於創建 PDF 文件或 PDF 頁面屬性以及讀取 PDF 文件。 它將包含示例和實用的代碼片段,讓您從實踐中了解如何在 Python 專案中使用 IronPDF for Python 的 PdfWriter 編寫 PDF 文件並創建新的 PDF 頁面。

設定 IronPDF

安裝

要開始使用 IronPDF,您需要透過 Python 包索引來安裝它。 在終端中運行以下命令:

 pip install ironpdf

撰寫 PDF 文件及操作 PDF 文件

建立新 PDF

IronPDF 簡化了...的過程創建新的PDF文件以及處理現有的PDF檔案。 它提供了一個簡單的介面來生成文件,無論是簡單的單頁 PDF 還是包含用戶密碼等各種元素的更複雜的文件。 此功能對於報告生成、製作發票等任務至關重要。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
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>
"""
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("New PDF File.pdf")
PYTHON

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

輸出檔案

合併 PDF 檔案

IronPDF 簡化了將多個 PDF 文件合併為一個的任務。 此功能有助於彙總各種報告、組合掃描文件或組織相關信息。 例如,當您需要從多個來源創建綜合報告時,或者當您有一系列文件需要作為單一檔案呈現時,您可能需要合併 PDF 檔案。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdfOne = PdfDocument("Report First.pdf")
pdfTwo = PdfDocument("Report Second.pdf")
merged = PdfDocument.Merge(pdfOne, pdfTwo)
merged.SaveAs("Merged.pdf")
PYTHON

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

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

合併的 PDF 輸出

拆分單一 PDF

相反地,IronPDF 在將現有的 PDF 文件分割成多個新文件方面也表現出色。 此功能在需要從大型 PDF 文件中提取特定部分或將文件拆分為更小、更易管理的部分時非常便利。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# take the first page
page1doc = pdf.CopyPage(0)
page1doc.SaveAs("Split1.pdf")
PYTHON

例如,您可能希望從大型報告中獨立出某些 PDF 頁面,或從書籍的不同章節創建個別文件。 IronPDF 允許您選擇多個所需的頁面以轉換為新的 PDF 文件,確保您能夠根據需要操作和管理您的 PDF 內容。

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

分割 PDF 輸出

實現安全功能

在處理敏感或機密信息時,確保 PDF 文件的安全成為首要任務。 IronPDF 透過提供強大的安全功能來滿足此需求,包括用戶密碼保護和加密。 這確保您的 PDF 檔案保持安全,只有授權用戶可以存取。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# 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.SecuritySettings.OwnerPassword = "top-secret"  # password to edit the PDF
pdf.SecuritySettings.UserPassword = "sharable"  # password to open the PDF
pdf.SaveAs("secured.pdf")
PYTHON

透過實施使用者密碼,您可以控制誰可以查看或編輯您的 PDF 文件。 加密選項增加了額外的安全層,保護您的數據免於未經授權的訪問,並使IronPDF成為管理PDF格式敏感信息的可靠選擇。

從 PDF 提取文本

IronPDF 的另一個重要功能是能夠從 PDF 文件中提取文字。 此功能對於數據檢索、內容分析,或是將現有 PDF 的文字內容重用到新文檔中特別有用。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# Extract text from PDF document
allText = pdf.ExtractAllText()
# Extract text from specific page in the document
specificPage = pdf.ExtractTextFromPage(3)
PYTHON

無論是提取數據進行分析、在大型文檔中搜索特定信息,還是將內容從 PDF 轉換為文本文件以進行進一步處理,IronPDF 都使這一切變得簡單高效。 該庫可確保提取的文本保持其原有的格式和結構,使其可以立即用於您的特定需求。

管理文件資訊

PDF 的高效管理不僅限於其內容。 IronPDF 允許有效管理文件元數據和屬性,例如作者姓名、文件標題、創建日期等。 此功能對於組織和編目 PDF 文檔至關重要,尤其是在文檔來源和元數據很重要的環境中。

from ironpdf import *

License.LicenseKey = "Your-License-Key"
# Set a log path
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 = Now()
pdf.SaveAs("MetaData Updated.pdf")
PYTHON

例如,在學術或企業環境中,能夠追蹤文件的創建日期和作者對於記錄保存和文件檢索目的而言可能是必不可少的。 IronPDF 讓管理這些資訊變得簡單,提供了一種簡化的方式來處理和更新 Python 應用程式中的文件資訊。

結論

Python PdfWriter(代碼範例教程),圖4:許可證

許可證

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

IronPDF for Python 也提供以下功能:

*創建新的 PDF 檔案從頭開始使用 HTML 或 URL

*编辑現有的 PDF 文件

*旋轉PDF 頁面

*提取文字、元數據和圖片從 PDF 文件中

*安全 PDF 文件使用密碼和限制

*拆分合併PDFs

IronPDF for Python 提供 یک免費試用供用戶探索其功能。 若要在試用期後繼續使用,許可證價格從 $749 起算。 此定價允許開發人員在其專案中充分利用 IronPDF 的全部功能。

< 上一頁
如何在 Python 中給 PDF 檔案添加浮水印
下一個 >
如何在 Python 中從掃描的 PDF 提取文本

準備開始了嗎? 版本: 2024.9 剛剛發布

免費 pip 安裝 查看許可證 >