Export Range of Cells in a Worksheet to Image

Possible Usage Scenarios

You can make an image of a worksheet using Aspose.Cells for Python via .NET. However, sometimes you need to export only a range of cells in a worksheet to an image. This article explains how to achieve this.

Export Range of Cells in a Worksheet to Image

To take an image of a range, set the print area to the desired range and then set all margins to 0. Also set ImageOrPrintOptions.one_page_per_sheet to true. The following code takes an image of the range D8:G16. Below is a screenshot of the sample Excel file used in the code. You can try the code with any Excel file.

Screenshot of Sample Excel File and its Exported Image

todo:image_alt_text

Executing the code creates an image of the range D8:G16 only.

todo:image_alt_text

Sample Code

from aspose.cells import Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Create workbook from source file.
workbook = Workbook(sourceDir + "sampleExportRangeOfCellsInWorksheetToImage.xlsx")
# Access the first worksheet
worksheet = workbook.worksheets[0]
# Set the print area with your desired range
worksheet.page_setup.print_area = "D8:G16"
# Set all margins as 0
worksheet.page_setup.left_margin = 0.0
worksheet.page_setup.right_margin = 0.0
worksheet.page_setup.top_margin = 0.0
worksheet.page_setup.bottom_margin = 0.0
# Set OnePagePerSheet option as true
options = ImageOrPrintOptions()
options.one_page_per_sheet = True
options.image_type = ImageType.JPEG
options.horizontal_resolution = 200
options.vertical_resolution = 200
# Take the image of your worksheet
sr = SheetRender(worksheet, options)
sr.to_image(0, outputDir + "outputExportRangeOfCellsInWorksheetToImage.jpg")