IronPDF for Python - 使用 Python 腳本建立、編輯和讀取 PDF 文件
IronPDF 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 官方網站下載並安裝最新版本的 Python 3.x:https://www.python.org/downloads/。 在安裝過程中,請務必選擇將 Python 新增至系統 PATH 的選項,這樣就可以從命令列存取它。
- 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 *應用許可證密鑰
接下來,透過將許可證密鑰分配給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"將 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")將 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提供許可和支持
購買許可證即可在實際項目中使用。 這裡也提供 30 天試用許可證。
如需查看完整的程式碼範例、教學課程、許可資訊和文檔,請造訪: IronPDF for Python 。
如需更多協助或有任何疑問,請聯絡我們的團隊。







