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 cloudmersive_convert_api_client.rest 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!
![](https://miro.medium.com/v2/resize:fit:700/1*DSHV4CcOOgX9eeTxQ_KXfw.jpeg)