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 Python,請確保電腦已安裝以下先決軟體:

  1. .NET 6.0 SDK:IronPDF Python 依賴 IronPDF .NET 庫,特別是 .NET 6.0,作為其底層技術。因此,有必要安裝 .NET 6.0 SDK 安裝 IronPDF Python 前需要在您的機器上安裝以下軟體。

  2. Python: 從官方 Python 網站下載並安裝最新版本的 Python 3.x: https://www.python.org/downloads/

在安裝過程中,請確保選擇將 Python 添加到系統 PATH 的選項,這樣可以從命令行訪問它。

  1. Pip: 從 Python 3.4 開始,Pip 通常捆綁在 Python 安裝中。然而,根據您的 Python 安裝,您可能需要檢查是否已安裝 pip 或單獨安裝它。

  2. IronPDF Library: 可通過 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 statement for IronPDF Python
from ironpdf import *
PYTHON

應用許可證密鑰

接下來,通過將有效的許可證或試用密鑰指派給 License 對象的 LicenseKey 屬性來應用許可證密鑰。在 import 語句之後,並在使用任何 IronPDF 方法之前,包含以下代碼:

License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
PYTHON

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

將 HTML 渲染為 PDF

利用 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF 檔案。以下代碼將簡單的 HTML 字串轉換為 PDF 檔案:

from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

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

# Export to a file or Stream
pdf.SaveAs("html_to_pdf.pdf")
PYTHON

將 URL 轉換為 PDF

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

from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

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

# Export to a file or Stream
pdf.SaveAs("url_to_pdf.pdf")
PYTHON

紀錄

若要啟用紀錄,請使用以下代碼片段:

Logger.EnableDebugging = True
Logger.LogFilePath = "Default.log"
Logger.LoggingMode = Logger.LoggingModes.All
PYTHON

授權及支援服務

購買授權 適用於實際專案。也提供30天試用許可證。 這裡若要查看我們完整的程式碼範例、教學、授權資訊及文件

請訪問: IronPDF for Python更多支持和查詢,請 請詢問我們的團隊.