IronPDF for Python - 使用 Python 腳本建立、編輯和讀取 PDF 文件

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF Python 入門

IronPDF for Python 是由 Iron Software 開發的一個強大的函式庫,它為軟體工程師提供了在 Python 3 專案中建立、編輯和提取 PDF 內容的功能。

IronPDF for Python是在IronPDF for .NET的成功和流行基礎上建立的。

使用 IronPDF for Python

先決條件

若要使用 IronPDF for Python,請確保電腦已安裝以下必備軟體:

  1. .NET 6.0 SDK :IronPDF for Python 依賴 IronPDF .NET 函式庫,特別是 .NET 6.0 作為其底層技術。 因此,要使用 IronPDF for Python,您的電腦上必須安裝.NET 6.0 SDK
  2. Python :從 Python 官方網站下載並安裝最新版本的 Python 3.x:https://www.python.org/downloads/。 在安裝過程中,請務必選擇將 Python 新增至系統 PATH 的選項,這樣就可以從命令列存取它。
  3. Pip :從 Python 3.4 及更高版本開始,Pip 通常與 Python 安裝捆綁在一起。 但是,根據你的 Python 安裝情況,你可能需要檢查 pip 是否已經安裝,或單獨安裝它。
  4. IronPDF 庫:可以透過 pip 新增 IronPDF 庫。使用以下命令透過 pip 安裝 IronPDF:
pip install ironpdf

若要安裝特定版本的 IronPDF,請使用下列語法:"==2023.xx"。 例如,您可以執行指令"pip install ironpdf==2023.xx"。

在某些系統中,Python 2.x 可能仍然是預設版本。 在這種情況下,您可能需要明確使用pip3命令而不是pip ,以確保您使用的是 Python 3 的 pip。

潛在的安裝問題

對於以下任一問題,請參考故障排除文章來解決:

由於作業系統錯誤,無法安裝軟體包。 -未能找到 IronPdf.Slim.dll

開始編寫 Python 程式碼

使用下列語句匯入產生和處理 PDF 文件所需的必要模組。 請確保在 Python 腳本的開頭導入它們。

# Import necessary components from the IronPDF library
from ironpdf import *
# Import necessary components from the IronPDF library
from ironpdf import *
PYTHON

應用許可證密鑰

接下來,透過將許可證密鑰分配給License物件的LicenseKey屬性,將有效的許可證密鑰或試用密鑰應用於 IronPDF。 在導入語句之後、使用任何 IronPDF 方法之前,先加入以下程式碼:

# Apply the license key for IronPDF
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Apply the license key for IronPDF
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
PYTHON

請注意請確保在呼叫任何 IronPDF 方法之前完成所有設定、日誌記錄和許可操作。

將 HTML 渲染為 PDF

使用RenderHtmlAsPdf方法將 HTML 字串轉換為 PDF 文件。 以下程式碼將簡單的 HTML 字串轉換為 PDF 文件:

from ironpdf import *

# Instantiate ChromePdfRenderer
renderer = ChromePdfRenderer()

# Create a PDF from an HTML string
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the generated PDF to a file
pdf.SaveAs("html_to_pdf.pdf")
from ironpdf import *

# Instantiate ChromePdfRenderer
renderer = ChromePdfRenderer()

# Create a PDF from an HTML string
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the generated PDF to a file
pdf.SaveAs("html_to_pdf.pdf")
PYTHON

將 URL 轉換為 PDF

使用RenderUrlAsPdf方法將 URL 或本機檔案路徑轉換為 PDF 文件。 以下是一個範例:

from ironpdf import *

# Instantiate ChromePdfRenderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")

# Save the generated PDF to a file
pdf.SaveAs("url_to_pdf.pdf")
from ironpdf import *

# Instantiate ChromePdfRenderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")

# Save the generated PDF to a file
pdf.SaveAs("url_to_pdf.pdf")
PYTHON

日誌記錄

若要啟用日誌記錄,請使用下列程式碼片段:

# Enable debugging for logging
Logger.EnableDebugging = True

# Specify the log file path
Logger.LogFilePath = "Default.log"

# Set the logging mode to log all activities
Logger.LoggingMode = Logger.LoggingModes.All
# Enable debugging for logging
Logger.EnableDebugging = True

# Specify the log file path
Logger.LogFilePath = "Default.log"

# Set the logging mode to log all activities
Logger.LoggingMode = Logger.LoggingModes.All
PYTHON

提供許可和支持

購買許可證即可在實際項目中使用。 這裡也提供 30 天試用許可證。

如需查看完整的程式碼範例、教學課程、許可資訊和文檔,請造訪: IronPDF for Python

如需更多協助或有任何疑問,請聯絡我們的團隊

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Version: 2025.9 剛發表