IronPDF for Python - 在 Python 腳本中建立、編輯及讀取 PDF 檔案
IronPDF for Python 簡介
IronPDF for Python 是由 Iron Software 開發的一套強大函式庫,讓軟體工程師能夠在 Python 3 專案中建立、編輯及擷取 PDF 內容。
IronPDF for Python 延續了 IronPDF for .NET 的成功與廣受歡迎。
IronPDF for Python 的主要功能:
- 從 HTML、URL、JavaScript、CSS 及各種圖像格式生成 PDF 檔案
- 在 PDF 中加入頁首/頁尾、簽名、附件,以及密碼與安全設定
- 透過完整的多執行緒與非同步支援來優化效能
- 瀏覽我們的網站,探索超過 50 項功能的完整清單,並查閱眾多程式碼範例
使用 IronPDF for Python
先決條件
若要使用 IronPDF for Python,請確保電腦已安裝以下必要軟體:
- .NET 6.0 SDK:IronPDF for Python 採用 IronPDF .NET 函式庫(具體為 .NET 6.0 版本)作為其底層技術。 因此,若要使用 IronPDF for Python,您的電腦上必須已安裝 .NET 6.0 SDK。
- Python:請從 Python 官方網站 https://www.python.org/downloads/ 下載並安裝最新版本的 Python 3.x。 在安裝過程中,請務必選取將 Python 加入系統 PATH 的選項,如此一來即可透過命令列存取 Python。
- Pip:自 Python 3.4 及後續版本起,Pip 通常已內建於 Python 安裝包中。 不過,視您的 Python 安裝環境而定,您可能需要確認 pip 是否已安裝,或需另行安裝。
- IronPDF 函式庫:可透過 pip 安裝 IronPDF 函式庫。請使用以下指令透過 pip 安裝 IronPDF:
pip install ironpdf
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 *套用授權金鑰
接著,請將有效的授權金鑰或試用金鑰套用至 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"將 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")將 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")記錄
若要啟用記錄功能,請使用以下程式碼片段:
# 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提供授權與技術支援
如需查看完整的程式碼範例、教學指南、授權資訊及文件,請造訪:IronPDF for Python。
如需更多支援或有任何疑問,請聯繫我們的團隊。







