How to get a file format icon for any file type in Python

Icons are the meat and potatoes of any good UI experience. But finding high quality and consistent icons for a large number of specific file formats can be a real struggle. Add in the need for pixel perfection, and you’ve got a recipe for serious frustration. And what if you need these icons to be implemented dynamically in your project? That can introduce all sorts of additional problems. Instead, let’s use a handy Cloudmersive API that will fetch specific file format icons at custom sizes on demand.

We begin with pip installation, as so:

pip install cloudmersive-convert-api-client

Now call the function for file type icons. You should specify your file extension in 4 or fewer alphanumeric characters, and optionally pick a desired size (width in pixels).

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))file_extension = 'file_extension_example' # str | Required; The file extension to be used for the icon. Limited to 4 AlphaNumeric characters.icon_size = 56 # int | Optional; The desired width of the icon, preserving its aspect ratio. (optional)try:# Get PNG icon byte array for the file extensionapi_response = api_instance.convert_document_get_file_type_icon_advanced(file_extension, icon_size=icon_size)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_get_file_type_icon_advanced: %s\n" % e)

And there you go, file icons solved!

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store