跳過到頁腳內容
使用 IRONPDF FOR PYTHON

如何在 Python 中生成 PDF 報告

產生 PDF 文件報告是數據分析和數據科學家的常見需求。 IronPDF 是一個功能強大的庫,它允許在 Python 程式碼應用程式中建立 PDF 表格文件,類似於 PHP 中的 FPDF 庫。本教學將指導您如何使用 IronPDF 從 HTML 範本或 URL 建立和寫入 PDF 報告。如果操作不當,這個過程可能會非常耗時。

IronPDF:Python PDF 庫

IronPDF是一個功能全面的庫,專為 Python 應用程式設計,用於建立 PDF、編輯 PDF 文件以及從 PDF 文件中提取內容。 它是一款功能強大的工具,能夠滿足軟體工程師的需求,他們經常面臨從各種資料來源或範本產生 PDF 文件最終結果的挑戰。 透過 IronPDF,使用者可以輕鬆地將 HTML 內容或 URL 轉換為 PDF 文件,操作 PDF 內容,並將這些功能整合到 Python 程式碼專案中,使其成為任何處理 PDF 生成和操作任務的 Python 開發人員必不可少的程式庫。

IronPDF 還允許您建立互動式表單分割合併PDF 文件、從 PDF 文件中提取文字和圖像、在 PDF 文件中搜尋特定單字、將 PDF 頁面柵格化為圖像以及列印 PDF 文件

先決條件

第一步是確保您符合以下先決條件:

  1. 您的系統上已安裝 Python 3.7 或更高版本。
  2. 由於 IronPDF 庫依賴 .NET 6.0 作為其底層技術,因此需要安裝 .NET 6.0 執行環境。

您可以從官方.NET 下載頁面安裝 .NET 6.0 執行階段。

安裝

要使用 IronPDF,您需要透過 pip 安裝該軟體包:

pip install ironpdf
pip install ironpdf
SHELL

如何在 Python 中產生 PDF 報告,圖 1:安裝 IronPDF 安裝 IronPDF

IronPDF 首次運行時會自動下載其他依賴項。

建立一個簡單的 PDF 文檔

以下是一個使用 HTML 範本產生簡單 PDF 文件的範例程式碼:

from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render an HTML string as a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the rendered PDF to a file
pdf.SaveAs("hello_world.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render an HTML string as a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the rendered PDF to a file
pdf.SaveAs("hello_world.pdf")
PYTHON

這段程式碼片段將 HTML 字串轉換為 PDF 文件,並將其保存在與 Python 腳本相同的資料夾中。

從 URL 產生 PDF

IronPDF 也可以透過以下範例程式碼,根據網頁 URL 建立 PDF 檔案:

from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render a URL as a PDF document
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")

# Save the rendered PDF to a file
pdf.SaveAs("website_snapshot.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render a URL as a PDF document
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")

# Save the rendered PDF to a file
pdf.SaveAs("website_snapshot.pdf")
PYTHON

使用數據框產生 PDF 報告

使用 IronPDF 和 Python 可以輕鬆建立專業美觀的 PDF 報告。 以下是如何使用詳細的資料框和自訂 HTML 樣式產生增強型報表的方法:

步驟 1:導入庫

首先,您需要匯入所需的庫。 IronPDF 中的ChromePdfRenderer對於 PDF 產生過程至關重要。 該程式庫能夠將 HTML 內容轉換為 PDF 文件。 此外,還需要匯入 pandas(一個功能強大的資料操作庫),用於建立和管理資料框。 我們將使用 Pandas 將報告資料整理成表格形式。

from ironpdf import ChromePdfRenderer
import pandas as pd
from ironpdf import ChromePdfRenderer
import pandas as pd
PYTHON

步驟 2:許可證密鑰配置

啟動 IronPDF 需要設定您的許可證密鑰。 這一步驟至關重要,因為它解鎖了 IronPDF 的所有功能,使您可以產生沒有任何浮水印或限制的 PDF 檔案。 這是專業使用該庫的一個簡單但至關重要的步驟。

License.LicenseKey = "Your-License-Key"
License.LicenseKey = "Your-License-Key"
PYTHON

步驟 3:建立資料框

在這裡,你將使用 Pandas 建立一個資料框。 此資料框將作為報告的資料來源。 提供的範例包含詳細的員工信息,展示了 Pandas 在處理和建立複雜數據集方面的能力。 您可以根據要建立的報告的具體要求來自訂資料框。

data = {
    'Employee ID': [101, 102, 103, 104],
    'Name': ['John Doe', 'Alice Smith', 'Bob Johnson', 'Emily Davis'],
    'Age': [28, 34, 45, 29],
    'Department': ['Sales', 'HR', 'IT', 'Marketing'],
    'City': ['New York', 'London', 'San Francisco', 'Berlin']
}
df = pd.DataFrame(data)
data = {
    'Employee ID': [101, 102, 103, 104],
    'Name': ['John Doe', 'Alice Smith', 'Bob Johnson', 'Emily Davis'],
    'Age': [28, 34, 45, 29],
    'Department': ['Sales', 'HR', 'IT', 'Marketing'],
    'City': ['New York', 'London', 'San Francisco', 'Berlin']
}
df = pd.DataFrame(data)
PYTHON

步驟 4:設計 HTML 模板

在這一步驟中,您將設計一個帶有 CSS 樣式的 HTML 模板。 此範本定義了 PDF 報告的視覺呈現方式。 CSS樣式可以增強報表中呈現資料的視覺吸引力和可讀性。 資料框動態插入到此 HTML 範本中是透過 Python 的字串格式化功能實現的。

# HTML styling for the PDF report
html_style = """
<html>
<head>
<style>
  table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    padding: 5px;
    text-align: left;
  }
  th {
    background-color: #f2f2f2;
  }
</style>
</head>
<body>
  <h2>Company Employee Report</h2>
  {table}
</body>
</html>
"""

# Replace {table} with the HTML representation of the data frame
html_content = html_style.format(table=df.to_html(index=False, border=0))
# HTML styling for the PDF report
html_style = """
<html>
<head>
<style>
  table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    padding: 5px;
    text-align: left;
  }
  th {
    background-color: #f2f2f2;
  }
</style>
</head>
<body>
  <h2>Company Employee Report</h2>
  {table}
</body>
</html>
"""

# Replace {table} with the HTML representation of the data frame
html_content = html_style.format(table=df.to_html(index=False, border=0))
PYTHON

步驟 5:渲染並儲存 PDF

最後,使用 IronPDF 的 ChromePdfRenderer 將 HTML 內容轉換為 PDF 文件。 RenderHtmlAsPdf 方法處理 HTML 和 CSS,將其轉換為 PDF 檔案。然後使用 SaveAs 函數儲存此文件,從而產生格式良好、美觀的 PDF 報告。 此步驟概括了轉換過程,將資料和範本合併成最終文件。

# Render the HTML string to a PDF document
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html_content)

# Save the rendered PDF to a file
pdf.SaveAs("enhanced_employee_report.pdf")
# Render the HTML string to a PDF document
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html_content)

# Save the rendered PDF to a file
pdf.SaveAs("enhanced_employee_report.pdf")
PYTHON

輸出

以下是PDF格式的輸出報告:

如何在 Python 中產生 PDF 報告,圖 2:公司員工報告 公司員工報告

建立一個簡單的 PDF 文檔

以下是使用 HTML 範本產生簡單 PDF 文件的範例程式碼:

from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render an HTML string as a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the rendered PDF to a file
pdf.SaveAs("hello_world.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render an HTML string as a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

# Save the rendered PDF to a file
pdf.SaveAs("hello_world.pdf")
PYTHON

這段程式碼片段將 HTML 字串轉換為 PDF 文件,並將其保存在與 Python 腳本相同的資料夾中。

從 URL 產生 PDF

IronPDF 也可以透過以下範例程式碼,根據網頁 URL 建立 PDF 檔案:

from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render a URL as a PDF document
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")

# Save the rendered PDF to a file
pdf.SaveAs("website_snapshot.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer using the ChromePdfRenderer class
renderer = ChromePdfRenderer()

# Render a URL as a PDF document
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")

# Save the rendered PDF to a file
pdf.SaveAs("website_snapshot.pdf")
PYTHON

這將保存指定網頁的 PDF 快照。

結論

IronPDF 是一款功能強大的工具,可供 Python 開發人員和資料科學家產生 PDF 報告。 按照本指南,您可以輕鬆地將 PDF 生成功能整合到您的 Python 專案中,無論您是從 HTML 範本、URL 還是資料框建立 PDF。 請務必查閱 IronPDF 的豐富文件和範例,以充分利用其功能完成 PDF 任務,例如新增圓餅圖。

不斷嘗試 IronPDF 提供的各種功能和選項,以建立滿足您需求的 PDF 報告。 只要方法得當,看似耗時的任務就能變成高效率且自動化的工作流程的一部分。

IronPDF 提供免費試用,用戶可以在購買前充分體驗其各項功能。 此外,它對開發用途免費,為開發人員在開發階段提供了一種經濟高效的解決方案。 對於商業部署,IronPDF 的許可起價為$799 ,可滿足專業和企業級需求。

常見問題解答

如何從 Python 中的 HTML 模板產生 PDF 報告?

使用 IronPDF,您可以使用 ChromePdfRenderer 類從 HTML 模板生成 PDF 報告。這個類別允許您將 HTML 內容渲染成 PDF,並使用 SaveAs 方法儲存。

在 Python 中使用 IronPDF 生成 PDF 的前提条件是什么?

要在 Python 中使用 IronPDF 生成 PDF,請確保您已安裝 Python 3.7 或更新版本,以及 .NET 6.0 運行時,該運行時可從官方 .NET 下載頁面下載。

如何在 Python 環境中安裝 IronPDF?

IronPDF 可以使用 pip 包管理器安裝在您的 Python 環境中。在您的終端執行指令 pip install ironpdf,必要的套件就會安裝完成。

我可以用 Python 從資料框架建立 PDF 嗎?

是的,您可以使用 IronPDF 從 Python 中的資料框架建立 PDF。導入必要的函式庫,建立您的資料框架,設計代表資料的 HTML 模板,並使用 ChromePdfRenderer 來渲染並儲存 PDF。

是否可以用 Python 將 URL 轉換成 PDF 文件?

IronPDF 允許您使用 ChromePdfRenderer 類將 URL 轉換成 PDF 文件。您可以使用此類別將 URL 渲染成 PDF,並將文件保存到檔案中。

Python 中的 IronPDF 库有哪些高级功能?

IronPDF 提供先進的功能,例如建立互動式表格、分割和合併 PDF 檔案、擷取文字和影像、在 PDF 內搜尋、將頁面光柵化成影像,以及列印 PDF 檔案。

IronPDF in Python 是否提供免費試用?

是的,IronPDF 提供免費試用版,您可以探索其功能。該試用版也可免費用於開發目的,使其成為您專案初期的經濟之選。

如何排除在 Python 中生成 PDF 的問題?

如果您在使用 IronPDF 生成 PDF 时遇到问题,请确保您安装了正确版本的 Python 和 .NET runtime。此外,請檢查所有必要的相依性是否已正確安裝與設定。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。