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,作為其底層技術。 因此,為了使用 IronPDF Python,必須在您的機器上安裝 .NET 6.0 SDK

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

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

  3. Pip:Pip 通常從 Python 3.4 開始與 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 statement for IronPDF Python
from ironpdf import *
py
PYTHON

套用授權金鑰

接下來,將有效的授權或試用金鑰應用到IronPDF,方法是將授權金鑰分配給License物件的LicenseKey屬性。 在 import 語句後立即加入以下程式碼,在使用任何 IronPDF 方法之前:

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

[{i:(確保在調用任何 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")
py
PYTHON

將網址轉換成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")
py
PYTHON

日誌記錄

要啟用記錄功能,請使用以下程式碼片段:

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

授權與支持可用

購買許可證 以用於實時項目。 30 天試用授權也可以在此處獲得。

請參閱我們完整的代碼示例、教程、許可信息和文檔列表。

訪問:IronPDF for Python

如需更多支持和查詢,請詢問我們的團隊