Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created July 6, 2024 11:52
Show Gist options
  • Save aspose-com-gists/eb9a075e183b721f9f41e66365b6fa6e to your computer and use it in GitHub Desktop.
Save aspose-com-gists/eb9a075e183b721f9f41e66365b6fa6e to your computer and use it in GitHub Desktop.
Convert XPS to BMP in Python
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])])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment