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:
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.
Use the ImageOrPrintOptions: Configure the image options to specify the desired image format and other settings.
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.
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.
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)
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?
@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.
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:
@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’
@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.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.