IronPDF for Python - 在 Python 腳本中建立、編輯及讀取 PDF 檔案

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

IronPDF for 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 官方網站 https://www.python.org/downloads/ 下載並安裝最新版本的 Python 3.x。 在安裝過程中,請務必選取將 Python 加入系統 PATH 的選項,如此一來即可透過命令列存取 Python。
  3. Pip:自 Python 3.4 及後續版本起,Pip 通常已內建於 Python 安裝包中。 不過,視您的 Python 安裝環境而定,您可能需要確認 pip 是否已安裝,或需另行安裝。
  4. IronPDF 函式庫:可透過 pip 安裝 IronPDF 函式庫。請使用以下指令透過 pip 安裝 IronPDF:
 pip install ironpdf

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

請注意在某些系統上,Python 2.x 可能仍是預設版本。 在這種情況下,您可能需要明確使用 pip3 指令,而非 pip,以確保您使用的是 Python 3 的 pip。

潛在的安裝問題

若遇到以下任一問題,請參閱疑難排解文章以解決:

開始撰寫 Python 程式碼

請使用以下語句導入生成與處理 PDF 文件所需的模組。 請務必在 Python 腳本的開頭導入這些函式庫。

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

套用授權金鑰

接著,請將有效的授權金鑰或試用金鑰套用至 IronPDF,方法是將授權金鑰指派給 License 物件的 LicenseKey 屬性。 請在 import 語句之後、使用任何 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

using 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 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎?
執行範例 觀看您的 HTML 轉為 PDF。