
Aspose.Page for Python via .NET 使您可以輕鬆查看、處理和將 XPS 文件轉換為其他廣泛使用的文件格式,這些格式幾乎與所有流行的操作系統高度兼容。是的,我們將在基於 Python 的項目中以程式方式 將 XPS 轉換為 PDF。現在,您將能夠開發一個 XPS 轉 PDF 轉換器 來自動化此文件格式轉換。因此,我們強烈建議您選擇這個 Python 庫,因為它非常開發者友好並提供易於調用的方法。所以,讓我們直接進入 XPS 文件轉換的實現。
以下要點將涵蓋:
XPS 文件轉換 - API 安裝
要安裝這個 Python 庫,您可以 下載 最新版本,或在終端/CMD 中運行以下 pip 命令:
pip install aspose-page

在 Python 中將 XPS 轉換為 PDF - 代碼範例
我們有源 XPS 文件以實現此功能。
請按照以下步驟 將 XPS 轉換為 PDF:
- 初始化輸入和輸出文件的流。
- 通過創建 XpsDocument 類的對象從流中加載 XPS 文檔。
- 創建 PdfSaveOptions 類的實例。
- 通過設置 jpeg_quality_level 屬性的值來設置圖像的壓縮質量級別。
- text_compression 屬性指定用於所有內容流(除圖像外)的壓縮類型。
- 設置 page_numbers 屬性以設置要轉換的頁面數字數組。
- 通過創建 PdfDevice 類的對象來為 PDF 格式創建渲染設備。
- 調用 save 方法以將 XPS 轉換為 PDF。
一旦您運行以下代碼範例,它將 將 XPS 轉換為 PDF 並將其保存到磁碟中:
from aspose.page.xps import * | |
from aspose.page.xps.presentation.pdf import * | |
data_dir = "./working-files" | |
# Initialize streams for input and output files. | |
with open(data_dir + "XPStoPDF_out.pdf", "wb") as pdf_stream: | |
with open(data_dir + "input.xps", "rb") as xps_stream: | |
# Load the XPS document from the stream by creating an object of the XpsDocument class. | |
document = XpsDocument(xps_stream, XpsLoadOptions()) | |
# Create an instance of the PdfSaveOptions class. | |
options = PdfSaveOptions() | |
# Set the quality level of compression for an image by setting a value of jpeg_quality_level property. | |
options.jpeg_quality_level = 100 | |
options.image_compression = PdfImageCompression.JPEG | |
# text_compression property specifies compression type to be used for all content streams except images. | |
options.text_compression = PdfTextCompression.FLATE | |
# Set the page_numbers property to set the array of numbers of pages to convert. | |
options.page_numbers = [ 1, 2 ] | |
# Create rendering device for PDF format by creating an object of the class. | |
device = PdfDevice(pdf_stream) | |
# Call the save method to convert XPS to PDF. | |
document.save(device, options) |
您可以在下面的圖像中看到上述代碼範例的輸出:

線上 XPS 到 PDF 轉換器
Aspose.Page 還提供了一個 線上 工具來穩健地將 XPS 轉換為 PDF。此外,有多種選項可以上傳源文件,並提供簡單的拖放介面。最重要的是,您可以放置雲端託管的 XPS 文件的 URL 以進行文件格式轉換。

將 XPS 轉換為 PDF - 獲取免費許可證
您可以獲得免費的 臨時許可證 來嘗試這個 Python 庫,無需評估限制。
總結
我們在這裡結束這篇博客文章。您可以通過訪問 文檔、API 參考和 GitHub 存儲庫來了解有關此 XPS 文件轉換 API 的更多信息。因此,我們介紹了如何 在 Python 中將 XPS 轉換為 PDF。此外,我們還稍微介紹了線上 XPS 到 PDF 轉換器。aspose.com 將會在任何其他有趣的主題上發表另一篇博客文章。因此,請關注定期更新。
提問
您可以在我們的 論壇 上告訴我們您的問題或查詢。
常見問題 – FAQs
我該如何將 XPS 文件轉換為 PDF?
您可以通過安裝 Aspose.Page for Python via .NET 自動化 XPS 文件轉換。此外,請遵循此 鏈接。
我該如何打開 .XPS 文件?
這個 線上 XPS 查看器由 Aspose.Page 提供支持。它是基於網頁的,您可以輕鬆查看 XPS 文件。