Package: windows-fonts
windows-fontsEnumerate and discover fonts on WindowsWhy this module existsMost (all?) python modules that render text to an image (matplotlib, PIL/Pillow etc) need to take afilenameon Windows, but happily take a font name on other platforms, which is a) annoying from a cross-platform
standpoint, and b) requires a bit of "faff" for the user to discover the font file for a given font.Synopsis>>>fromwindows_fontsimportFontCollection,Weight>>>fonts=FontCollection()>>># Get the first variant (light/regular/bold etc) for a named family>>>family=fonts['Arial']>>>variant=family[0]>>>variant<FontVariantname=Regular,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.REGULAR>>>>print(variant.filename,variant.weight,variant.style)C:\WINDOWS\FONTS\ARIAL.TTFWeight.REGULARStyle.NORMAL>>># Find the "closest" variant for a given family>>>variant=family.get_best_variant(weight=Weight.BOLD)# Or `style=Style.ITALIC, or both>>>variant<FontVariantname=Bold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD>>>># Or to find all "matching" variants in priority order:>>>forvariantinfamily.get_matching_variants(weight=Weight.BOLD):...variant...<FontVariantname=Bold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD><FontVariantname=Bold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD><FontVariantname=Black,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BLACK><FontVariantname=NarrowBold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD><FontVariantname=NarrowBold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD><FontVariantname=BoldItalic,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD><FontVariantname=ItalicBold,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD><FontVariantname=NarrowBoldItalic,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD><FontVariantname=NarrowItalicBold,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD>Find a specific Font VariantSome font families are aggregated, for instance Arial and Arial Narrow both are placed under the "Arial" family by the Win32 APIs. But if you want to be able to directly get to the Arial Narrow font files you need to use the top-levelget_matching_variantsfunction:>>>get_matching_variants(win32_family_names="Arial Narrow")[<FontVariantname=Narrow,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.REGULAR>,<FontVariantname=NarrowItalic,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.REGULAR>,<FontVariantname=NarrowBold,family=<FontFamilyname="Arial">,style=Style.NORMALweight=Weight.BOLD>,<FontVariantname=NarrowBoldItalic,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD>]Or if you know the full name already:>>>get_matching_variants(full_name="Arial Narrow Bold Italic")[<FontVariantname=NarrowBoldItalic,family=<FontFamilyname="Arial">,style=Style.ITALIICweight=Weight.BOLD>]Get information about a Font Variant.informationis a dict-like object. The some keys will not be available on every font.>>>info=variant.information>>>importpprint>>>pprint.pprint(dict(info)){'copyright':'© 2008 The Monotype Corporation. All Rights Reserved.','description':...,'designer':'Robin Nicholas, Patricia Saunders','full_name':'Arial Narrow Italic','license_description':...,'manufacturer':'The Monotype Corporation','postscript_name':'ArialNarrow-Italic','preferred_family_names':'Arial','preferred_subfamily_names':'Narrow Italic','trademark':'Arial is a trademark of The Monotype Corporation in the United ''States and/or other countries.','typographic_subfamily_names':'Narrow Italic','typographic_family_names':'Arial','versions':'Version 2.40','win32_family_names':'Arial Narrow','win32_subfamily_names':'Italic'}RequirementsPython >= 3.7Windows Vista and upSome functions or methods need Windows 10 (get_matching_variantstop-level function, andFontFamily.get_matching_variantswhen called withwidth,slant,optical_size, oritalicparameters).