在 Python 中將 XML 轉換為 PDF |將 XML 檔案轉換為 PDF

隨著資料驅動應用程式日益突出,將資料從 XML 檔案匯出為普遍接受的 PDF 格式的需求變得越來越重要。這篇部落格文章提供了在 Python 中有效地將 XML 轉換為 PDF 的逐步指南。那麼就讓我們開始吧!

本文將涵蓋以下主題:

  1. 將 XML 轉換為 PDF 的 Python 函式庫
  2. 在 Python 中將 XML 轉換為 PDF
  3. 在 Python 中從 XML 產生 PDF
  4. 在線將 XML 轉換為 PDF
  5. 免費學習資源

將 XML 轉換為 PDF 的 Python 函式庫

要從 XML 產生 PDF 文件,最直接的方法是利用專為 XML 到 PDF 轉換而設計的 Aspose.PDF for Python 庫。安裝和使用簡單,它為將 XML 文件轉換為 PDF 文件提供了高效的解決方案。 Aspose.PDF for Python 是 PDF 生成、操作和轉換的強大解決方案,為開發人員提供了對文件工作流程的無與倫比的控制。

下載軟體包或在控制台中使用以下 pip 命令從 PyPI 安裝 API:

> pip install aspose-pdf

在 Python 中將 XML 轉換為 PDF

我們可以按照以下步驟輕鬆將 XML 轉換為 PDF:

  1. 建立 Document 類別的物件。
  2. 透過提供 XML 檔案路徑,使用 Document.bindxml(file) 方法綁定 XML。
  3. 使用 Document.save(outputfilename) 方法將 XML 轉換為 PDF。

以下程式碼範例示範如何使用 Python 將 XML 檔案轉換為 PDF。

import aspose.pdf as ap

# 建立新的 PDF 文檔
pdfDocument = ap.Document();

# 轉換和綁定 XML
pdfDocument.bind_xml( "C:\\Files\\sample.xml");

# 從 XML 產生 PDF
pdfDocument.save( "C:\\Files\\generated-pdf.pdf");

來源 XML 文件

以下是我們用來轉換為 PDF 文件的範例 XML 檔案。

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
  <Page>
    <TextFragment>
      <TextSegment>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla odio lorem, luctus in lorem vitae, accumsan semper lectus. Cras a auctor leo, et tincidunt lacus.</TextSegment>
    </TextFragment>
  </Page>
</Document>

產生的 PDF 文件

在 Python 中將 XML 轉換為 PDF

在 Python 中將 XML 轉換為 PDF

在 Python 中從 XML 產生 PDF

我們也可以從包含應用程式資料的 XML 檔案產生 PDF 文件。為此,我們首先使用 XSLT 將其轉換為 Aspose.PDF 相容的 XML,然後將其轉換為 PDF 格式。

以下是我們需要轉換為 PDF 文件的範例 XML 資料。

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <cd>
	<Content>Hello World!</Content>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
  <cd>
    <title>Eros</title>
    <artist>Eros Ramazzotti</artist>
    <country>EU</country>
    <company>BMG</company>
    <price>9.90</price>
    <year>1997</year>
  </cd>
</catalog>

為了使該資料與 Aspose.PDF XML 相容,我們將執行 XSLT 轉換。為此,我們將在 XSLT 樣式表檔案中定義一個模板,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
  <Document xmlns="Aspose.Pdf">
  <Page>
	 <PageInfo IsLandscape="false" Height="595" Width="420">
      <Margin Top="71" Bottom="71" Left="28" Right="28" />
    </PageInfo>
    <Header>
        <Margin Top="20" />
        <Table ColumnAdjustment="AutoFitToWindow">
            <Row>
                <Cell Alignment="1">
                    <TextFragment>
                        <TextSegment>Date: 11/01/2024</TextSegment>
                    </TextFragment>
                </Cell>
                <Cell Alignment="3">
                    <TextFragment>
                        <TextSegment>Page $p / $P</TextSegment>
                    </TextFragment>
                </Cell>
            </Row>
        </Table>
    </Header>
    <HtmlFragment>
      <![CDATA[
        <h1 style="font-family:Tahoma; font-size:16pt;">My CD Collection</h1>
        ]]>
    </HtmlFragment>
    <TextFragment>
      <TextSegment>Welcome</TextSegment>
    </TextFragment>
	<Table ColumnAdjustment="AutoFitToWindow" ColumnWidths ="10 10 10 10">
      <DefaultCellPadding Top="5" Left="0" Right="0" Bottom="5" />
      <Border>
        <Top Color="Black"></Top>
        <Bottom Color="Black"></Bottom>
        <Left Color="Black"></Left>
        <Right Color="Black"></Right>
      </Border>
      <Margin Top="15" />
      <Row BackgroundColor="LightGray" MinRowHeight="20">
        <Border>
          <Bottom Color="Black"></Bottom>
        </Border>
        <Cell Alignment="2">
          <TextFragment>
            <TextSegment>Title</TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment>Artist</TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment>Price</TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment>Year</TextSegment>
          </TextFragment>
        </Cell>
      </Row>
	  <xsl:for-each select="catalog/cd">
		<Row>
        <Cell Alignment="2">
          <TextFragment>
            <TextSegment><xsl:value-of select="title"/></TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment><xsl:value-of select="artist"/></TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment><xsl:value-of select="price"/></TextSegment>
          </TextFragment>
        </Cell>
        <Cell>
          <TextFragment>
            <TextSegment><xsl:value-of select="year"/></TextSegment>
          </TextFragment>
        </Cell>
      </Row>
	  </xsl:for-each>
    </Table>
  </Page>
</Document>
</xsl:template>
</xsl:stylesheet>

建立模板檔案後,我們可以按照前面提到的步驟產生 PDF。但是,我們只需要透過提供 XML 檔案和 XSLT 檔案路徑來呼叫 Document.bindxml(xmlfile, xslfile) 方法。

以下程式碼範例示範如何使用 Python 從 XML 檔案產生 PDF。

import aspose.pdf as ap

# 建立新的 PDF 文檔
pdfDocument = ap.Document();

# 轉換和綁定 XML
pdfDocument.bind_xml( "C:\\Files\\data.xml",  "C:\\Files\\template.xslt");

# 從 XML 產生 PDF
pdfDocument.save( "C:\\Files\\generated-pdf-table.pdf");
在 Python 中從 XML 產生 PDF

在 Python 中從 XML 產生 PDF

XML 到 PDF 轉換器許可證

您可以取得臨時許可證來使用該 API,而不受評估限制。

在線將 XML 轉換為 PDF

您也可以使用這個免費的 XML 到 PDF 轉換器 工具在線上將 XML 文件轉換為 PDF 文件。

XML 檔案轉 PDF – 學習資源

除了將 XML 文件建立為 PDF 文件之外,您還可以了解有關創建、操作和轉換 PDF 文件的更多信息,並使用以下資源探索該庫的各種其他功能:

結論

在本文中,您學習如何在 Python 中將 XML 轉換為 PDF。透過遵循本文中概述的步驟,您可以輕鬆地將此功能整合到您的 Python 應用程式中,以從 XML 生成 PDF 文件。如果您有任何疑問,請隨時透過我們的免費支援論壇告訴我們。

也可以看看