
- 本节介绍如何使用Aspose.Page for Python via .NET将XPS转换为BMP。
- 本节将介绍此Python库的安装过程。
- 在这里,我们将通过编写代码片段实现构建XPS文件转换器的功能。
- 在最后一部分,我们将介绍一个用于XPS到BMP转换的在线工具。
这是另一篇关于Aspose.Page类别的精彩博客文章,我们将学习两种文件格式之间的另一种丰富转换。因此,本指南将主要集中在使用企业级API进行的编程XPS到BMP转换,适用于Python开发人员。此外,我们还将深入探讨API参考和Aspose.Page for Python via .NET提供的方法调用。因此,请坚持阅读本文至最后,并为您的企业软件复制它。让我们开始吧。
XPS到图像 - API安装
此Python库脱颖而出的原因之一是它提供了强大而无忧的安装。因此,请继续下载最新版本,或者您可以在终端/CMD中运行以下pip命令:
pip install aspose-page
在Python中将XPS转换为BMP - 代码示例
现在,进入实现部分,我们将演示如何通过编写几行Python源代码来开发XPS到BMP转换器。然而,您需要有一个源XPS文件来测试该功能。
您可以按照以下步骤和代码示例以编程方式在Python中将XPS转换为BMP:
- 通过初始化XpsDocument类从流中加载XPS文档。
- 初始化BmpSaveOptions类的对象。
- 设置smoothing_mode属性的值。
- 设置resolution和page_numbers属性的值。
- 创建ImageDevice类的实例。
- 调用save方法保存文件。
- 遍历文档分区(在XPS术语中为固定文档),并将文件保存为磁盘上的BMP图像。
import aspose | |
from aspose.page.xps import * | |
from aspose.page.xps.presentation.image import * | |
import os | |
# The path to the documents directory. | |
data_dir = "C:\\Desktop\\" | |
# Input file | |
input_file_name = data_dir + "input.xps" | |
#Outut file | |
output_file_name = data_dir + "XPStoBMP_out.bmp" | |
# Initialize XPS input stream | |
with open(input_file_name, "rb",) as xps_stream: | |
# Load the XPS document from the stream by initializing the XpsDocument class. | |
document = XpsDocument(xps_stream, XpsLoadOptions()) | |
# Initialize an object of the BmpSaveOptions class. | |
options = BmpSaveOptions() | |
# Set the value of the smoothing_mode property. | |
options.smoothing_mode = aspose.pydrawing.drawing2d.SmoothingMode.HIGH_QUALITY | |
# Set the values of resolution and page_numbers properties. | |
options.resolution = 300 | |
options.page_numbers = [ 1, 2, 6 ] | |
# Create an instance of the ImageDevice class. | |
device = ImageDevice() | |
# Invoke the save method to save the file. | |
document.save(device, options) | |
# Iterate through document partitions (fixed documents, in XPS terms) | |
for i in range(len(device.result)): | |
# Iterate through partition pages | |
for j in range(len(device.result[i])): | |
# Initialize image output stream | |
with open(os.path.splitext(output_file_name)[0] + "_" + str(i + 1) + "_" + str(j + 1) + | |
os.path.splitext(output_file_name)[1], "wb") as image_stream: | |
# Save the file as a BMP image on the disk. | |
image_stream.write(device.result[i][j][0:0+len(device.result[i][j])]) |

XPS文件转换器 - 在线工具
我们已经介绍了实现XPS到图像转换的编程方式。接下来,我们将介绍一个由Aspose.Page提供的在线工具。它是免费的,您可以利用其无缝的转换和操作功能。最重要的是,您可以通过多种选项上传源文件。

获取免费许可证
您可以获得一个免费临时许可证,以超越评估限制尝试此Python API。
总结
我们在此结束本指南,希望您觉得这篇文章值得阅读。它涵盖了步骤并演示了XPS到BMP转换的代码示例。此外,您可以访问文档、API参考和GitHub库以进行进一步探索。此外,Aspose.Page为多种编程语言提供解决方案,您可以在这里找到。感谢您的时间,我们将再次带来另一个指南。请继续关注aspose.com。
随时与我们联系
您可以通过我们的论坛告诉我们您的问题或疑问。