There’s no shortage of file formats to choose from when we’re ready to share finished Excel documents. We can publish our spreadsheets as PDFs, export them as CSV files, or even convert our reports and graphs to a series of lightweight screenshots to post them online.
So, with all those good Excel conversion options out there, why would we bother converting Excel files to HTML?
The great thing about HTML is its flexibility - not only is it web accessible (like a PDF), but it’s also a highly interactive format we can code forms, filters, and dynamic charts into, enhancing the experience of folks viewing our reports. It’s just as portable as CSV files, too, and it’s easy to slice & dice; we can embed certain tables from our HTML spreadsheet content into our web pages (e.g., blogs) without including the entire document.
In this tutorial, we’ll learn how to easily convert Excel files to HTML format using just a few lines of Python code. Let’s get started!
Step 1: Install the SDK
In our first step, we’re going to install the client SDK using a pip install command:
pip install cloudmersive-convert-api-client
Step 2: Add the Imports
Now we can add the below imports to the top of our controller:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
Step 3: Configuration
Configuring this API is going to involve providing a Cloudmersive API key to authorize our requests. If we don’t have an API key, we can get a free one easily - we’ll just need to create a free account on the Cloudmersive website. Our free API key will allow a limit of 800 API calls per month and zero commitments.
Once we have our API key, we can add the following configuration snippet to our file and paste our API key within it:
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Step 4: Create an Instance of the API
In our final step, we’re going to create an instance of the Excel to HTML API and call the document conversion function. We can now load our file in the input_file
variable:
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Convert Excel XLSX Spreadsheet to HTML Document
api_response = api_instance.convert_document_xlsx_to_html(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_xlsx_to_html: %s\n" % e)
It’s worth noting that this API supports both XLSX and XLSB formats.
Conclusion
That’s all there is to it: no more code required! We can now easily make high fidelity conversions from Excel to HTML format with just a few lines of Python code.
If you have any questions about converting between document formats using Cloudmersive APIs, feel free to reach out - we’d love to hear from you!