錯誤:Python 中未定義模組

This article was translated from English: Does it need improvement?
Translated
View the article in English

您可能會遇到類似以下的警告:

  • "ChromePdfRenderer" 未定義
  • "PdfCssMediaType" 未定義
  • "FitToPaperModes" 未定義

上述警告可忽略。 由於 IronPDF for Python 採用 IronPDF C#,這些功能是在 .NET 6.0 中實現的。因此,相關的類別定義可能無法在 Python 環境中直接檢視或定義。

以下是一個範例,說明在使用 IronPDF 函式庫時,您可能會在 Python 中遇到並處理此類情況。

# Importing the IronPDF module. This is assumed to be a hypothetical Python wrapper for IronPDF C# library.
# In practice, you might use a Python package manager to install and import the necessary module.
from ironpdf import ChromePdfRenderer, PdfCssMediaType, FitToPaperModes

# Example function using IronPDF components to illustrate usage
def generate_pdf():
    try:
        # Create a new PDF renderer
        renderer = ChromePdfRenderer()

        # Define options or configurations for the renderer
        renderer.css_media_type = PdfCssMediaType.PRINT
        renderer.fit_to_paper_mode = FitToPaperModes.FIT

        # Assume we have HTML content to convert to PDF
        html_content = "<h1>Hello, World!</h1>"

        # Render the HTML content to PDF
        pdf_document = renderer.render_html_as_pdf(html_content)

        # Save the PDF document to a file
        pdf_document.save_as("output.pdf")

    except Exception as e:
        # Log and handle any exceptions that occur during PDF generation
        print(f"An error occurred: {e}")

# Execute the function to generate a PDF
generate_pdf()
# Importing the IronPDF module. This is assumed to be a hypothetical Python wrapper for IronPDF C&num; library.
# In practice, you might use a Python package manager to install and import the necessary module.
from ironpdf import ChromePdfRenderer, PdfCssMediaType, FitToPaperModes

# Example function using IronPDF components to illustrate usage
def generate_pdf():
    try:
        # Create a new PDF renderer
        renderer = ChromePdfRenderer()

        # Define options or configurations for the renderer
        renderer.css_media_type = PdfCssMediaType.PRINT
        renderer.fit_to_paper_mode = FitToPaperModes.FIT

        # Assume we have HTML content to convert to PDF
        html_content = "<h1>Hello, World!</h1>"

        # Render the HTML content to PDF
        pdf_document = renderer.render_html_as_pdf(html_content)

        # Save the PDF document to a file
        pdf_document.save_as("output.pdf")

    except Exception as e:
        # Log and handle any exceptions that occur during PDF generation
        print(f"An error occurred: {e}")

# Execute the function to generate a PDF
generate_pdf()
PYTHON

說明:

  • 導入語句:程式碼假設存在 IronPDF 的 Python 封裝函式庫或模組 (ironpdf)。 實際部署時,需透過套件管理員安裝相應模組。
  • 錯誤處理:函式 generate_pdf() 配備 try-except 區塊,用於擷取並處理因 Python 中未定義的類別而可能發生的例外狀況。
  • PDF 渲染:使用 ChromePdfRenderer 及其他類別,說明若可存取 Python 介面時,通常如何設定選項並渲染 PDF 文件。

註:所提供的程式碼為假設性範例,僅供說明之用,假設存在 IronPDF for Python 的封裝函式庫。 實際的實作細節可能因函式庫支援狀況及與 .NET 元件的整合方式而有所不同。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎?
執行範例 觀看您的 HTML 轉為 PDF。