Python を使用したプレゼンテーションのフォント置換の構成
概要
フォント置換を使用すると、プレゼンテーションのレンダリングまたは変換時にアクセスできないフォントの代わりに、使用可能なフォントを Aspose.Slides が使用できるようになります。置換はレンダリングされた出力にのみ影響し、プレゼンテーション コンテンツに割り当てられたフォントは変更されません。
特定のフォントが利用できない場合に使用するフォントを定義でき、Aspose.Slides がレンダリング中に行う置換を確認できます。これにより、インストールされているフォントが異なる環境間でも出力を一貫させることができます。
フォント置換の取得
Use the FontsManager.get_substitutions method to determine which fonts will be substituted when the presentation is rendered. The method returns FontSubstitutionInfo objects that identify the original and substituted font names.
The following Python example lists all font substitutions for a presentation:
import aspose.slides as slides
with slides.Presentation("Presentation.pptx") as presentation:
for substitution in presentation.fonts_manager.get_substitutions():
print(f"{substitution.original_font_name} -> {substitution.substituted_font_name}")
選択スライドのフォント置換の取得
Use FontsManager.get_substitutions with a list of slide indexes to inspect only the substitutions required to render specific slides. This is useful when you are rendering or exporting part of a presentation, checking a large presentation incrementally, locating slides that depend on unavailable fonts, preparing a minimal font package for a server or container, or diagnosing rendering differences without processing unrelated slides.
The list contains one-based slide indexes: 1 identifies the first slide. By contrast, the Presentation.slides collection is zero-based, so that same slide is accessed as presentation.slides[0]. Keep this difference in mind when building the list to avoid off‑by‑one errors.
Call the method through the Presentation.fonts_manager property. It returns only the substitutions determined while rendering the selected slides. Each result is a FontSubstitutionInfo object containing the original and substituted font names. The result reflects the current font environment, configured fallback rules, substitution rules stored in an IFontSubstRuleCollection, and externally loaded fonts.
The same substitution can be required by more than one selected slide. Deduplicate the results when you create a font inventory or preflight report. The following example reports every returned substitution and then creates a sorted list of unique font mappings:
import aspose.slides as slides
with slides.Presentation("Presentation.pptx") as presentation:
selected_slides = [1, 3, 5]
substitutions = list(presentation.fonts_manager.get_substitutions(selected_slides))
print("Substitutions for the selected slides:")
for substitution in substitutions:
print(f"{substitution.original_font_name} -> {substitution.substituted_font_name}")
preflight_entries = [f"{substitution.original_font_name} -> {substitution.substituted_font_name}" for substitution in substitutions]
unique_preflight_entries = {entry.casefold(): entry for entry in preflight_entries}
sorted_preflight_entries = sorted(unique_preflight_entries.values(), key=str.casefold)
print("Deduplicated font preflight report:")
for entry in sorted_preflight_entries:
print(entry)
The FontsManager class provides both forms of the method. Choose one according to the scope of the rendering operation:
| Method call | Use it when |
|---|---|
| get_substitutions with no arguments | You need substitutions for the entire presentation. |
| get_substitutions with a list of slide indexes | You need substitutions for a selected range, incremental check, or partial export. |
フォント置換ルールの設定
To specify the font that Aspose.Slides should use when a source font is unavailable:
- Load the presentation.
- Create font definitions for the source and substitute fonts.
- Create a FontSubstRule with the WHEN_INACCESSIBLE condition.
- Add the rule to a FontSubstRuleCollection.
- Assign the collection to the FontsManager.font_subst_rule_list property.
- Render or convert the presentation.
The following Python example substitutes Arial for SomeRareFont when SomeRareFont is unavailable, and then renders the first slide to verify the result. The substitute font must be available to Aspose.Slides.
import aspose.slides as slides
with slides.Presentation("Fonts.pptx") as presentation:
source_font = slides.FontData("SomeRareFont")
substitute_font = slides.FontData("Arial")
substitution_rule = slides.FontSubstRule(source_font, substitute_font, slides.FontSubstCondition.WHEN_INACCESSIBLE)
substitution_rules = slides.FontSubstRuleCollection()
substitution_rules.add(substitution_rule)
presentation.fonts_manager.font_subst_rule_list = substitution_rules
with presentation.slides[0].get_image(1, 1) as image:
image.save("slide.jpg", slides.ImageFormat.JPEG)
Note
プレゼンテーション全体で使用されるフォントを無条件に変更する場合は、Font Replacement を参照してください。数式フォントの制限事項
Font substitution rules are part of the standard font selection process used during rendering and conversion. They work for regular text when Aspose.Slides can replace an inaccessible font with the available font specified by a rule.
Office Math equations have an additional requirement. If an equation uses Cambria Math, Aspose.Slides may need that exact font to calculate and render the equation layout. A rule that substitutes another math font, such as STIX Two Math, cannot replace Cambria Math for this purpose, and rendering may still report that Cambria Math is required.
To render or convert such a presentation, make Cambria Math available to Aspose.Slides. Install it in the operating system or load it as an external font.
This limitation applies to equation layout. The substitution rules described above still apply to regular presentation text.
よくある質問
フォント置換とフォント置換(replacement)の違いは何ですか?
Font replacement はプレゼンテーション全体でフォントを別のフォントに意図的に変更します。フォント置換は、元のフォントが利用できないなどの条件が満たされたときに、レンダリングされた出力用にフォントを選択します。
置換ルールはいつ適用されますか?
ルールはレンダリングおよび変換時のfont selection sequenceに参加します。WHEN_INACCESSIBLE の場合、ソースフォントにアクセスできないときのみルールが使用されます。
フォントが欠落していて置換ルールが設定されていない場合、何が起こりますか?
Aspose.Slides はフォント選択プロセスに従って最も近い利用可能なフォントを選択します。結果はランタイム環境にインストールされているフォントに依存します。
外部フォントをロードして置換を回避できますか?
はい。外部フォントをロード すれば、レンダリングおよび変換時に Aspose.Slides がそれらを使用できます。
Aspose はライブラリにフォントを同梱していますか?
いいえ。フォントの提供とライセンス遵守はユーザーの責任です。
Windows、Linux、macOS 間で置換結果が異なることはありますか?
はい。各 OS のインストールフォント及びフォント検索パスが異なるため、あるマシンで利用できるフォントが別のマシンでは置換が必要になることがあります。
バッチ変換でフォント選択を一貫させるにはどうすればよいですか?
すべてのマシンまたはコンテナで同一のフォントファイルとバージョンを使用し、必要な外部フォントをロードし、ライセンスが許可する場合はフォントを埋め込むことを推奨します。また、エクスポート前に FontsManager.get_substitutions を呼び出して予期しない置換を特定できます。