在 Python 中将 XPS 转换为图像

本文详细介绍了如何使用 Python 将 XPS 转换为 Image。它包含分步工作流程和可运行的代码,用于在 Python 中将 XPS 导出为 JPG。此外,它还涵盖了不同的属性,以根据您的要求自定义流程。

使用 Python 将 XPS 转换为图像的步骤

  1. 通过安装 Aspose.Page 设置环境以转换 XPS 文件
  2. 使用 XpsDocument 类通过流或文件加载 XPS 文档
  3. 初始化 PngSaveOptions 类对象
  4. 为图像创建渲染设备
  5. 遍历文档分区并写入输出图像

这些步骤总结了使用 Python 将 XPS 文件转换为 JPG 的细节。从磁盘或流中快速加载输入 XPS 文件并设置所需的参数。最后,以您喜欢的文件格式(如 JPG、PNG 等)渲染输出图像。

使用 Python 将 XPS 导出为 PNG 的代码

import aspose.page
from aspose.page import *
from aspose.page.xps import *
from aspose.page.xps.presentation.image import *
from io import BytesIO
import os
output_file_name = "XPStoImage_out.png"
# Initialize XPS input stream
with open("input.xps", "rb",) as xps_stream:
# Load XPS document
document = aspose.page.xps.XpsDocument(xps_stream, XpsLoadOptions())
# Initialize PngSaveOptions object
options = PngSaveOptions()
options.smoothing_mode = aspose.pydrawing.drawing2d.SmoothingMode.HIGH_QUALITY
options.resolution = 300
options.page_numbers = [1]
# Create rendering device
device = ImageDevice()
document.save(device, options)
# Iterate document partitions
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:
# Write image
image_stream.write(device.result[i][j][0:0+len(device.result[i][j])])

此示例代码描述了可以设置哪些方法和属性以在 Python 中将 XPS 渲染为 PNG。它主要与 XpsDocument 类配合使用以加载源文件。随后,在导出生成的图像之前,可以使用 PngSaveOptions 类设置各种自定义属性,例如平滑模式、分辨率、特定页码等。

本教程介绍了使用 Python 将 XPS 导出为图像 的信息。如果您有兴趣将 EPS 转换为图像格式,请前往 在 Python 中将 EPS 转换为图像 上的文章。

 简体中文