Aspose - excel to pdf, excel to image, excel to html in python

Package used:
pip install aspose-cells-python

Sample Excel file used for below conversions -
Sample_Excel.zip (9.9 KB)

Image conversion code –
from aspose.cells import Workbook
workbook = Workbook(“Sample_Excel.xlsx”)
workbook.save(“Sample_Excel.png”)

Output and issue – See below the attached image, the entire width of excel sheet is not exported to image.
image.png (14.9 KB)

PDF conversion code –
from aspose.cells import Workbook
workbook = Workbook(“Sample_Excel.xlsx”)
workbook.save(“Sample_Excel.pdf”)

Output and issue – See below the attached pdf, the entire width of excel sheet is not exported to pdf.
Sample_Excel.pdf (85.7 KB)

To resolve this width error we tried using ImageOrPrintOptions - but seems like it is not working in python code, getting error, even after installing the package with the above mentioned pip install command => ModuleNotFoundError: No module named ‘aspose.cells.ImageOrPrintOptions’; module ‘aspose.cells’ has no submodule ‘ImageOrPrintOptions’

HTML conversion code –
from aspose.cells import SaveFormat, Workbook
wb = Workbook(“Sample_Excel.xlsx”)
wb.save(“./Sample_Html.html”, SaveFormat.HTML)

Output and issue – See below the screenshot of html, the entire width of excel sheet is covered however, text within cell is touching the closed cell border while in excel there seem to be some padding.
image.jpg (60.0 KB)

@NehaPawar

Cause

The issues you’re experiencing with the conversions from Excel to PDF, image, and HTML formats are likely due to the default settings used during the conversion process. Specifically, the width of the Excel sheet may not be fully captured in the output formats, and the HTML output lacks padding within the cells.

Solution

  1. Image Conversion: To ensure the entire width of the Excel sheet is exported to an image, you should use the ImageSaveOptions class to specify the desired settings. This allows you to control the output dimensions and scaling.

  2. PDF Conversion: Similar to the image conversion, you can use PdfSaveOptions to adjust the page size and scaling to ensure the full content is captured.

  3. HTML Conversion: For the HTML output, you can use HtmlSaveOptions to set padding and other formatting options to improve the appearance of the text within the cells.

Code

Here’s how you can implement these changes in your code:

from aspose.cells import Workbook, ImageSaveOptions, PdfSaveOptions, HtmlSaveOptions

# Load the workbook
workbook = Workbook("Sample_Excel.xlsx")

# Image Conversion
image_options = ImageSaveOptions()
image_options.set_image_format("png")
image_options.set_one_page_per_sheet(True)  # Ensures each sheet is saved as a separate image
workbook.save("Sample_Excel.png", image_options)

# PDF Conversion
pdf_options = PdfSaveOptions()
pdf_options.set_page_size("A4")  # Set the desired page size
workbook.save("Sample_Excel.pdf", pdf_options)

# HTML Conversion
html_options = HtmlSaveOptions()
html_options.set_export_active_sheet_only(True)  # Export only the active sheet
html_options.set_image_options(image_options)  # Use the same image options for consistency
workbook.save("Sample_Html.html", html_options)

Next Steps

  1. Test the updated code to see if the output meets your expectations.
  2. Adjust the ImageSaveOptions, PdfSaveOptions, and HtmlSaveOptions further based on your specific requirements for layout and formatting.
  3. If you encounter any errors, ensure that you have the latest version of the Aspose.Cells library installed.

Please implement these changes and let me know if you need further assistance!

Sources:
[1]: Convert Excel to Pdf, Image and other formats - Aspose Documentation

Hi,
Thanks for your quick response.
I tried the code that you mentioned. But still getting error -

image_options.set_image_format(“png”)
AttributeError: ‘aspose.cells.ImageSaveOptions’ object has no attribute ‘set_image_format’

Then commented this line, then got this error -

image_options.set_one_page_per_sheet(True) # Ensures each sheet is saved as a separate image
AttributeError: ‘aspose.cells.ImageSaveOptions’ object has no attribute ‘set_one_page_per_sheet’

And got errors for other conversions as well -

pdf_options.set_page_size(“A4”) # Set the desired page size
AttributeError: ‘aspose.cells.PdfSaveOptions’ object has no attribute ‘set_page_size’

html_options.set_export_active_sheet_only(True) # Export only the active sheet
AttributeError: ‘aspose.cells.HtmlSaveOptions’ object has no attribute ‘set_export_active_sheet_only’

@NehaPawar,

Thanks for the sample files.

We will check each of your issues using the provided resource file(s) and respond with our findings and detailed feedback.

Any updates on this please?

@NehaPawar,

Please note when you render image from Excel spreadsheet, only the first page would be rendered. For your needs, you may render all the pages from the Excel sheet.

For your issues:
1). If you could take the print preview of the sheet in MS Excel, you will notice that there are 20 pages and not all columns would be rendered on the page. Aspose.Cells would work the same way as per the print preview of the sheet (MS Excel) when rendering to images or PDF. For your requirements, you may try to use all_columns_in_one_page_per_sheet Boolean option. You may try the following sample code:

import aspose.cells
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender
from aspose.cells import Workbook, WorksheetCollection,  Worksheet, Cells, CellsHelper, License, PdfSaveOptions, HtmlSaveOptions, SaveFormat, PageSetup, HtmlHiddenColDisplayType, HtmlHiddenRowDisplayType
....
workbook = Workbook("e:\\test2\\Sample_Excel.xlsx")
sheet = workbook.worksheets[0]

options = ImageOrPrintOptions()
options.horizontal_resolution = 200
options.vertical_resolution = 200
options.image_type =  ImageType.PNG
options.all_columns_in_one_page_per_sheet=True
#  Sheet2Image By Page conversion
sr = SheetRender(sheet, options)
for j in range(sr.page_count):
    sr.to_image(j,  "e:\\test2\\outPage_"  + str(j + 1) + ".png")

2). Please try the following sample code:

import aspose.cells
from aspose.cells import Workbook, WorksheetCollection,  Worksheet, Cells, CellsHelper, License, PdfSaveOptions, HtmlSaveOptions, SaveFormat, PageSetup, HtmlHiddenColDisplayType, HtmlHiddenRowDisplayType
....

workbook = Workbook("e:\\test2\\Sample_Excel.xlsx")
pdfSaveOptions = PdfSaveOptions()
pdfSaveOptions.all_columns_in_one_page_per_sheet = True

workbook.save("e:\\test2\\Sample_Excel1.pdf", pdfSaveOptions)

3). Your screenshot does not match with the Excel file you provided. Please provide your input Excel file that you are converting to HTML via Aspose.Cells for Python via .NET. We will check your issue soon.

Thanks for the response.
As per the changes you mentioned, with that image and pdf part - is working fine, thanks!
We wanted to have the content on a single page. So additionally, we used this - options.one_page_per_sheet=True


For HTML part, please consider this updated screenshot:
Screenshot 2025-06-24 192948.png (94.9 KB)

Issue - Text within cell is touching the cell border while in excel there seem to be some padding.

Attaching sample excel again -
Sample_Excel.zip (9.9 KB)

@NehaPawar,

It’s good to hear that the suggested code snippets are effective for converting your sheet to an image and for converting Excel to PDF.

I opened your template Excel file in MS Excel and observed a similar display/view as the HTML output rendered by Aspose.Cells for Python via .NET. Refer to the screenshot for details.
sc_shot_MS_Excel.jpg (625.8 KB)

Text within cell is touching the cell border in the generated HTML, while in excel there seem to be some padding

HTML: data is touching the cell border
Screenshot 2025-06-24 192948.png (94.9 KB)

Orignal Excel: there is some padding and data is not touching the cell border
image.png (7.7 KB)

Issue - Text within cell is touching the cell border while in excel there seem to be some padding


And one more thing,
although it pdf and image part is working correclty, getting warning for aspose.cells.drawing and aspose.cells.rendering
image.png (3.3 KB)

warnings:
Import “aspose.cells.drawing” could not be resolved from source
Import “aspose.cells.rendering” could not be resolved from source

I tried installing aspose-cells as well, still the same warning is there. Any suggestion on this as well please.

@NehaPawar,

Thanks for the screenshots.

I noticed this issue what you have talked about. But when I tested your scenario/case in MS Excel manually, I got the same results. Please open the Excel file into MS Excel and save it as “Web Page”. Now open the HTML file and you will find almost same display. Aspose.Cells follows MS Excel standards/specifications when reading/parsing or rendering HTMLs, so it is not an issue.

Yes, I notice this behavior. You may simply ignore this, it won’t do anything bad. And, we will look into it in details and get back to you with info.

Okay, got it. Thanks for your help.

@NehaPawar
If you run your code using PyCharm, the warnings will not appear.
Pycharm-noWarnnings.png (6.0 KB)
However, if you use Visual Studio Code, the issue you mentioned does occur. Regarding this problem, we have created a ticket (CELLSPYTHONNET-289).

Issue ID(s): CELLSPYTHONNET-289

For now, if you want to avoid the warnings, you can use PyCharm as your IDE.
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Okay, sure will check this. Thanks!

@NehaPawar,

You may try using PyCharm integrated development environment (IDE) for Python programming for the time being. And, we will be looking into the issue for Visual Studio code and try to fix it soon (if possible). Once we have an update on it, we will let you know here.

Sure, will try.

Thanks for all the support. A few more things I wanted to ask.

1. Execution time:
For the conversion of excel file mentioned earlier, it is taking time as:
excel to image → around 5 seconds
excel to pdf → around 2 seconds.

It includes just reading the file and then converting it to the required format, as mentioned in the earlier code. Is this the expected performance for these conversions? Or are there any recommended ways to optimize or speed up the conversion process?


2. Usage of Aspose.cells library in Lambda:
We are building an API (in python) using AWS Lambda and AWS API Gateway. And we want to use Aspose.cells for file conversion within the Lambda function.
Can you please guide us on:

  1. How to integrate and use Aspose.cells for python in an AWS Lambda environment?
  2. How to apply a temporary or permanent license in this setup?
  3. Are there any best practices or limitations we should be aware of when deploying Aspose in a serverless (Lambda) environment?
  4. If available, can you please share any official documentation or sample code related to this?

Thanks!

@NehaPawar

For the performance of rendering the template file to image or pdf, we will investigate it further to check whether there are some optimization can be made to improve it a bit.

For using the component in Lambda, there are some relevant topics:
How to Run Aspose.Cells in AWS Lambda|Documentation
How to Run Aspose.Cells for python via .NET in Docker|Documentation

We will investigate further to see whether we can provide some more specific information/documents about this topic for you and give feedback soon.

@NehaPawar,

1). I tested your scenario/case with Aspose.Cells for Python via .NET v25.6 using your template Excel file and with the following sample code, it works instantly and it took 1 second for sheet to image(s). It took less than 1 second (.4 second) for Excel to PDF conversion.
Sheet to image(s):

# Record the start time
start_time = time.time()
print(start_time)

workbook = Workbook("e:\\test2\\Sample_Excel.xlsx")
sheet = workbook.worksheets[0]

options = ImageOrPrintOptions()
options.horizontal_resolution = 200
options.vertical_resolution = 200
options.image_type =  ImageType.PNG
options.all_columns_in_one_page_per_sheet=True
#  Sheet2Image By Page conversion
sr = SheetRender(sheet, options)
for j in range(sr.page_count):
    sr.to_image(j,  "e:\\test2\\outputImage_"  + str(j + 1) + ".png")

# Record the end time
end_time = time.time()
# Calculate the elapsed time
elapsed_time = end_time - start_time
print(f"The operation took {elapsed_time:.4f} seconds.")

Excel to PDF:

# Record the start time
start_time = time.time()
print(start_time)

workbook = Workbook("e:\\test2\\Sample_Excel.xlsx")
pdfSaveOptions = PdfSaveOptions()
pdfSaveOptions.all_columns_in_one_page_per_sheet = True
workbook.save("e:\\test2\\out1.pdf", pdfSaveOptions)
# Record the end time
end_time = time.time()
# Calculate the elapsed time
elapsed_time = end_time - start_time
print(f"The operation took {elapsed_time:.4f} seconds.")

So, I got better and expected results regarding performance.

Hi @johnson.shi
Thanks for the links. But whatever links you have provided, those are for .net it seems.
Can you please provide me information on:

  1. How to integrate and use Aspose.cells for python in an AWS Lambda environment?
  2. How to apply a temporary or permanent license in this setup?

@NehaPawar

  1. Pease check Python | Tutorials, APIs, SDKs, Docs | AWS Developer Center
    We will try to install Python product in an AWS Lambda environment later.
  2. Licensing|Documentation

Hi All,

We tried to deploy the code in AWS lambda. Locally, it was working. But after deploying to lambda, we are getting this issue:

Process terminated. Couldn’t find a valid ICU package installed on the system. Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support.

Any suggestion on this please?

Thanks!