Convert range to image with cloud sdk [Python]

Is there a way to return an image of a range of cells based on a worksheet + cell range parameter?

Currently using the following but this only returns a png of the entire worksheet.

from asposecellscloud.requests import PutConvertWorkbookRequest
from asposecellscloud.apis.cells_api import CellsApi

api = CellsApi(client_id, client_secret)
remote_folder = "TestData/In"

local_name = "test_workbook.xlsx"
remote_name = "test_workbook.xlsx"

worksheet = "Sheet1"

request = PutConvertWorkbookRequest(
    file={remote_name: remote_name},
    format="png",
    sheet_name=worksheet,  # Optional: specific sheet name
)

temp_file = api.put_convert_workbook(request)

I’ve tried configuring the page setup configuration but this yields the same result.

from asposecellscloud.requests import PostPageSetupRequest
from asposecellscloud.models import PageSetup, ImageOrPrintOptions

page_setup = PageSetup(
    print_area="A1:H20",
    left_margin=0.0,
    right_margin=0.0,
    top_margin=0.0,
    bottom_margin=0.0,
)

request = PostPageSetupRequest(name=remote_name, folder=remote_folder, sheet_name="Sheet1", page_setup=page_setup)
page_setup_response = api.post_page_setup(request)
page_setup_response # {'code': 200, 'status': 'OK'}

Ideal case for us is providing range + worksheet to a workbook and exposing the same export apis
similar to: Export Range of Cells in a Worksheet to Image|Documentation
but using the cloud SDK.

@blake.woodford

To convert a specific range of cells to an image using the Aspose.Cells Cloud SDK in Python, you can follow a similar approach to what is done in the .NET and Java examples. The key is to set the print area for the worksheet before converting it to an image.

Here’s how you can achieve this:

  1. Set the Print Area: You need to define the print area for the worksheet to specify which range of cells you want to convert to an image.

  2. Use the ImageOrPrintOptions: Configure the image options to specify the desired image format and other settings.

  3. Render the Image: Finally, render the specified range to an image.

Here’s a sample code snippet that demonstrates this process:

from asposecellscloud.requests import PostPageSetupRequest, PutConvertWorkbookRequest
from asposecellscloud.apis.cells_api import CellsApi
from asposecellscloud.models import PageSetup, ImageOrPrintOptions

# Initialize the API
api = CellsApi(client_id, client_secret)
remote_folder = "TestData/In"
remote_name = "test_workbook.xlsx"

# Step 1: Set the print area
page_setup = PageSetup(
    print_area="A1:H20",  # Specify your desired range here
    left_margin=0.0,
    right_margin=0.0,
    top_margin=0.0,
    bottom_margin=0.0,
)

# Update the page setup for the specified worksheet
request = PostPageSetupRequest(name=remote_name, folder=remote_folder, sheet_name="Sheet1", page_setup=page_setup)
api.post_page_setup(request)

# Step 2: Convert the workbook to an image
image_options = ImageOrPrintOptions()
image_options.image_type = "png"  # Specify the image format

# Create a request to convert the workbook
convert_request = PutConvertWorkbookRequest(
    file={remote_name: remote_name},
    format="png",
    sheet_name="Sheet1",  # Specify the sheet name
)

# Step 3: Execute the conversion
temp_file = api.put_convert_workbook(convert_request)

# The temp_file will contain the image of the specified range

In this code:

  • The PostPageSetupRequest is used to set the print area for the specified range of cells.
  • The PutConvertWorkbookRequest is then used to convert the workbook to an image format, which will now only include the specified range.

Make sure to adjust the print_area to the range you want to export. This approach should help you achieve the desired functionality of exporting a specific range of cells to an image using the Aspose.Cells Cloud SDK.

If you have any further questions or need additional assistance, feel free to ask!

Where would these be used and passed through to?
I have tried to set the print area but when calling api.put_convert_workbook(convert_request) after it yields the same result and returns the entire worksheet.

@blake.woodford,

Please try the get_worksheet_with_format function.
You can specify the particular areas to output using the area parameter. Refer to the following code for guidance.

    remote_folder = 'TestData/In'
    local_name = 'Book1.xlsx'
    remote_name = 'Book1.xlsx'
    result = AuthUtil.Ready(self.api, local_name, remote_folder + '/' + remote_name ,  '')
    self.assertTrue(len(result. Uploaded)>0)     
    request =  GetWorksheetWithFormatRequest( remote_name, 'Sheet1',format= 'png',  area="I5:K6" ,folder= remote_folder,storage_name= '')
    temp_file  = self.api.get_worksheet_with_format(request)

This is great and works in most cases.

I’m still unable to configure to return the image along even when playing with the PageSetup settings before calling.

What ends up occurring is that the resulting image is on a standard A4 page with significant white space and has the row number / column letters added.

The following is what im working off of:

from asposecellscloud.apis.cells_api import CellsApi
from asposecellscloud.requests import UploadFileRequest, GetWorksheetWithFormatRequest, PostPageSetupRequest

# Initialize API
api = CellsApi("client_id", "client_secret")

# Example parameters
remote_folder = "TestData/In"
remote_name = "my_file.xlsx"
worksheet_name = "Sheet1"
area = "B9:AC50"
dpi = 400

# 1. Upload file
map_files = {"my_file.xlsx": "/path/to/local/file.xlsx"}
upload_request = UploadFileRequest(
    map_files, 
    f"{remote_folder}/{remote_name}", 
    storage_name=""
)
api.upload_file(upload_request)

# 2. Set page setup
page_setup_params = {
    "FitToPagesWide": 1,
    "FitToPagesTall": 1,
    "PrintHeadings": False,
    "CenterHorizontally": False,
    "CenterVertically": False,
    "Orientation": "Landscape",
    "RightMargin": 0,
    "TopMargin": 0,
    "BottomMargin": 0,
    "LeftMargin": 0,
    "OnePagePerSheet": True, # Note: this is not in the docs but tried anyways.
}

page_setup_request = PostPageSetupRequest(
    remote_name,
    worksheet_name,
    page_setup=page_setup_params,
    folder=remote_folder,
    storage_name="",
)
api.post_page_setup(page_setup_request)

# 3. Get worksheet as image
worksheet_request = GetWorksheetWithFormatRequest(
    remote_name,
    worksheet_name,
    format="png",
    area=area,
    folder=remote_folder,
    storage_name="",
    vertical_resolution=dpi,
    horizontal_resolution=dpi,
)
image_bytes = api.get_worksheet_with_format(worksheet_request)

@blake.woodford
Could you share your template file here ? We will check it soon.

Sure. Link to workbook here: OnedriveLink

Effectively trying to get image of ‘Retention Analysis’!B9:Z50

@blake.woodford
If you set
“FitToPagesWide”: 1,
“FitToPagesTall”: 1,
area = “B9:AC50” in MS Excel, you will get same result :
dest.png (30.4 KB)

we will check how to accept “OnePagePerSheet” in worksheet_request .

@wangtao

Appreciate it. In my results with the same PageSetup params I still have the white space around the png. I would think the margin params would remove this, but I might understand them wrong. Would it be possible if you could share your code to reproduce?

Thanks a ton!

@blake.woodford ,
Currently, the Cells Cloud exports cell area to image with row and column information, and the OnePagePerSheet parameter is false. We will make this change according to your requirements. In the upcoming version 25.2, which will be released next week, we will implement control over the OnePagePerSheet and PrintHeadings parameters. Please be patient for two or three days.

@blake.woodford ,

After carefully reviewing all the replies, I found that the solution provided by Professionalize.Discourse should address the issues you mentioned. I removed the unnecessary code and conducted a test. The results are shown in the figure below:

Please check if this meets your requirements. Here is the corresponding code snippet:

    pageSetup = PageSetup(bottom_margin= 0 ,right_margin= 0 ,left_margin= 0,top_margin =0,print_area= "B9:AC50" )
    request =  PostPageSetupRequest( 'ExampleData.xlsx', 'Retention Analysis', pageSetup, folder= remote_folder,storage_name= '')
    self.api.post_page_setup(request)
    request1 =  GetWorksheetWithFormatRequest( 'ExampleData.xlsx', 'Retention Analysis',format= 'png',folder= remote_folder,storage_name= '')
    self.api.get_worksheet_with_format(request1)

@blake.woodford,
In version 25.2, we have added a new feature: the API for converting range to Image.
Please refer to the code below: (SDK for Python v25.2.1 )

api = CellsApi(os.getenv(‘CellsCloudClientId’),os.getenv(‘CellsCloudClientSecret’),“v3.0”,os.getenv(‘CellsCloudApiBaseUrl’))
remote_folder = ‘TestData/In’

local_name = ‘ExampleData.xlsx’
remote_name = ‘ExampleData.xlsx’

mapFiles = {
local_name: local_name
}
request = UploadFileRequest( mapFiles, remote_folder + ‘/’ + remote_name,storage_name= ‘’)
api.upload_file(request)

image_or_print_options = ImageOrPrintOptions(one_page_per_sheet= True)
page_setup = PageSetup( black_and_white= True, bottom_margin=0,left_margin=0,top_margin=0,right_margin=0,print_headings= False )
range_operate_source = Range(column_count= 28 ,first_column= 1 ,first_row= 1 ,row_count= 42 )

range_convert = RangeConvertRequest(source= range_operate_source ,image_or_print_options= image_or_print_options ,page_setup= page_setup , image_type= ‘Png’ )

request = PostWorksheetCellsRangeToImageRequest( remote_name, ‘Retention Analysis’, range_convert,folder= remote_folder,storage_name= ‘’)
response = api.post_worksheet_cells_range_to_image(request)

This works flawlessly. Thank you so much for the help!

@blake.woodford
Thank you for your feedback. You are welcome. I’m glad your issue has been resolved. If you have any questions, please feel free to contact us at any time.