Embed Fonts in Presentations with Python
Introduction
Embedding fonts stores font data inside a PowerPoint presentation. When a viewer supports embedded fonts, it can display text using those fonts even if they are not installed on the target system. This helps preserve line breaks, text spacing, and slide layout.
Aspose.Slides for Python via .NET lets you retrieve, add, and remove embedded fonts through the fonts_manager property of a Presentation object. You can also reduce the size of embedded font data by removing characters that the presentation does not use.
The examples below work with PPTX files. Before embedding a font, make sure its font data is available to Aspose.Slides and its license permits embedding.
Get and Remove Embedded Fonts
Use get_embedded_fonts to list the fonts stored in a presentation. To remove one, pass a font from that list to remove_embedded_font, then save the presentation.
The following example lists the embedded fonts in EmbeddedFonts.pptx and removes Calibri if it is present:
import aspose.slides as slides
with slides.Presentation("EmbeddedFonts.pptx") as presentation:
fonts_manager = presentation.fonts_manager
embedded_fonts = fonts_manager.get_embedded_fonts()
for font in embedded_fonts:
print(font.font_name)
font_to_remove = next((font for font in embedded_fonts if font.font_name.casefold() == "calibri"), None)
if font_to_remove is not None:
fonts_manager.remove_embedded_font(font_to_remove)
presentation.save("WithoutEmbeddedCalibri.pptx", slides.export.SaveFormat.PPTX)
else:
print("Calibri is not embedded. No output file was created.")
Removing an embedded font removes its stored font data; it does not change the font assigned to the text. If the font is installed on the target system, the text can still use it. Otherwise, rendering may require font substitution, which can affect the layout.
Inspect Font Data and Embedding Permissions
Use the FontsManager class to inspect fonts before embedding them. Call get_fonts to retrieve the fonts used in the presentation. For each font, pass a FontData object and the required FontStyleType value to get_font_bytes. The method returns the binary data for that font style, or None when the requested font or style is unavailable. Do not pass a None result to get_font_embedding_level, because that method requires a byte array.
EmbeddingLevel is a flags enumeration that reports the embedding restrictions stored in the font:
INSTALLABLEpermits embedding and permanent installation on another system, subject to the font license.RESTRICTEDprohibits embedding unless permission is obtained from the font’s legal owner when it is the only usage-permission flag.PREVIEW_PRINTpermits temporary use for viewing and printing; a document containing the font must be read-only.EDITABLEpermits temporary use and allows the document to be edited and saved.NO_SUBSETTINGis an additional restriction that prohibits embedding only a subset of the glyphs. Embed all characters when this flag is present.BITMAP_ONLYis an additional restriction that permits only bitmap strikes to be embedded, not outline data. If the font has no bitmap strikes, it cannot be embedded.
The first four values describe usage permission, while NO_SUBSETTING and BITMAP_ONLY can be combined with them. Check the modifiers with bitwise operations. Because INSTALLABLE is zero, mask the usage-permission bits and compare the result with INSTALLABLE. Current fonts should set at most one usage-permission bit. For compatibility with older fonts that set more than one, the helper below selects the least restrictive permission: EDITABLE, then PREVIEW_PRINT, then RESTRICTED.
The following example audits the regular, bold, italic, and bold-italic data available for every font returned by get_fonts. It skips unavailable styles, restricted fonts, bitmap-only fonts, fonts limited to preview and print because the output remains editable, and fonts that are already embedded. If any available style has NO_SUBSETTING, it embeds all characters for that font family.
import aspose.slides as slides
def get_usage_permission(level):
permission_mask = slides.EmbeddingLevel.RESTRICTED | slides.EmbeddingLevel.PREVIEW_PRINT | slides.EmbeddingLevel.EDITABLE
permissions = level & permission_mask
if permissions & slides.EmbeddingLevel.EDITABLE:
return slides.EmbeddingLevel.EDITABLE
if permissions & slides.EmbeddingLevel.PREVIEW_PRINT:
return slides.EmbeddingLevel.PREVIEW_PRINT
if permissions & slides.EmbeddingLevel.RESTRICTED:
return slides.EmbeddingLevel.RESTRICTED
return slides.EmbeddingLevel.INSTALLABLE
with slides.Presentation("Fonts.pptx") as presentation:
fonts_manager = presentation.fonts_manager
font_styles = [slides.FontStyleType.REGULAR, slides.FontStyleType.BOLD, slides.FontStyleType.ITALIC, slides.FontStyleType.BOLD | slides.FontStyleType.ITALIC]
embedded_font_names = {font.font_name.casefold() for font in fonts_manager.get_embedded_fonts()}
embedding_plan = []
for font in fonts_manager.get_fonts():
if font.font_name.casefold() in embedded_font_names:
print(f"{font.font_name}: already embedded.")
continue
has_available_data = False
all_available_styles_can_be_embedded = True
preview_print_only = False
requires_full_font = False
for font_style in font_styles:
font_bytes = fonts_manager.get_font_bytes(font, font_style)
if font_bytes is None:
print(f"{font.font_name} ({font_style}): font data is unavailable.")
continue
has_available_data = True
embedding_level = fonts_manager.get_font_embedding_level(font_bytes, font.font_name)
usage_permission = get_usage_permission(embedding_level)
no_subsetting = bool(embedding_level & slides.EmbeddingLevel.NO_SUBSETTING)
bitmap_only = bool(embedding_level & slides.EmbeddingLevel.BITMAP_ONLY)
requires_full_font |= no_subsetting
preview_print_only |= usage_permission == slides.EmbeddingLevel.PREVIEW_PRINT
all_available_styles_can_be_embedded &= usage_permission != slides.EmbeddingLevel.RESTRICTED and not bitmap_only
print(f"{font.font_name} ({font_style}): {embedding_level}.")
if not has_available_data:
print(f"{font.font_name}: skipped because no requested style is available.")
elif not all_available_styles_can_be_embedded:
print(f"{font.font_name}: skipped because at least one available style does not permit outline embedding.")
elif preview_print_only:
print(f"{font.font_name}: skipped because this example produces an editable presentation.")
else:
rule = slides.export.EmbedFontCharacters.ALL if requires_full_font else slides.export.EmbedFontCharacters.ONLY_USED
embedding_plan.append((font, rule))
for font, rule in embedding_plan:
fonts_manager.add_embedded_font(font, rule)
presentation.save("WithAuditedFonts.pptx", slides.export.SaveFormat.PPTX)
This inspection reports the restrictions encoded in each font file. It does not grant a license, prove that you obtained the font legally, or replace checking the font’s license agreement before distributing an embedded copy.
Add Embedded Fonts
Use add_embedded_font to embed a font. Its overloads accept either a FontData object or a byte array containing the font data. The EmbedFontCharacters enumeration controls which characters are included:
- ALL embeds all characters in the font. Use this option when recipients need to edit the presentation and enter new text.
- ONLY_USED embeds only the characters used in the presentation to reduce file size. Choose this option for a finished presentation that is primarily intended for viewing.
The following example uses get_fonts to retrieve the fonts used in Fonts.pptx and embeds those that are not already embedded. The fonts to add must be available on the machine running the code. Existing embedded fonts retain their current character sets.
import aspose.slides as slides
with slides.Presentation("Fonts.pptx") as presentation:
fonts_manager = presentation.fonts_manager
all_fonts = fonts_manager.get_fonts()
embedded_fonts = fonts_manager.get_embedded_fonts()
embedded_names = {font.font_name.casefold() for font in embedded_fonts}
for font in all_fonts:
normalized_name = font.font_name.casefold()
if normalized_name not in embedded_names:
fonts_manager.add_embedded_font(font, slides.export.EmbedFontCharacters.ALL)
embedded_names.add(normalized_name)
presentation.save("WithEmbeddedFonts.pptx", slides.export.SaveFormat.PPTX)
Compress Embedded Fonts
compress_embedded_fonts reduces embedded font data by removing unused characters. It operates on fonts that are already embedded, so the size reduction depends on how much unused font data the presentation contains.
The following example compresses the fonts in EmbeddedFonts.pptx and saves the result as a separate file:
import aspose.slides as slides
with slides.Presentation("EmbeddedFonts.pptx") as presentation:
slides.lowcode.Compress.compress_embedded_fonts(presentation)
presentation.save("CompressedEmbeddedFonts.pptx", slides.export.SaveFormat.PPTX)
Keep the original file if recipients may need to add text later. Characters removed during compression are no longer available from the embedded font, even if you originally embedded all characters.
FAQ
How can I check whether an embedded font will still be substituted during rendering?
Call get_substitutions in the environment where you render the presentation to see which fonts Aspose.Slides will replace. Also check font substitution settings and font fallback rules. Fallback handles missing characters, so embedding a font does not resolve characters that the font itself does not contain.
Should I embed common fonts such as Arial and Calibri?
Base the decision on the target environment. If the required fonts are available on every machine that opens or renders the presentation, embedding them may add unnecessary file size. If recipients or servers may lack those fonts, embedding them can help preserve the intended appearance, provided their licenses allow it.