配置字体以渲染电子表格

可能的使用场景

Aspose.Cells for Python via .NET API 提供将电子表格渲染为图像格式以及转换为 PDF 和 XPS 格式的功能。为了最大化转换的准确性,确保电子表格中使用的字体可在操作系统的默认字体目录中找到。如果缺少所需字体,Aspose.Cells for Python via .NET API 将尝试用可用字体进行替代。

选择字体

下面是 Aspose.Cells for Python via .NET API 在后台执行的流程。

  1. API试图在文件系统中找到与电子表格中使用的确切字体名称匹配的字体。
  2. 如果API无法找到具有相同名称的精确字体,则尝试使用工作簿的DefaultStyle.font属性下指定的默认字体。
  3. 如果API无法找到工作簿的DefaultStyle.font属性下定义的字体,则尝试使用PdfSaveOptions.default_fontImageOrPrintOptions.default_font属性下指定的字体。
  4. 如果API无法找到PdfSaveOptions.default_fontImageOrPrintOptions.default_font属性下定义的字体,则尝试使用FontConfigs.default_font_name属性下指定的字体。
  5. 如果API无法找到FontConfigs.default_font_name属性下定义的字体,则尝试从所有可用字体中选择最合适的字体。
  6. 最后,如果API在文件系统中找不到任何字体,则使用Arial呈现电子表格。

设置自定义字体文件夹

Aspose.Cells for Python via .NET API 在操作系统的默认字体目录中搜索所需字体。若系统字体目录中没有,API 还会搜索用户定义的自定义目录。FontConfigs 类提供了多种设置自定义字体目录的方法,详见下文。

  1. FontConfigs.set_font_folder:如果只有一个要设置的文件夹,则此方法很有用。
  2. FontConfigs.set_font_folders:当字体存在于多个文件夹中,而用户希望将所有文件夹分开设置而不是将所有字体合并到一个文件夹中时,此方法很有用。
  3. FontConfigs.set_font_sources:当用户希望从多个文件夹加载字体或从字节数组加载单个字体文件或字体数据时,此机制很有用。
from aspose.cells import FileFontSource, FolderFontSource, FontConfigs, MemoryFontSource
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Defining string variables to store paths to font folders & font file
fontFolder1 = dataDir + "Arial"
fontFolder2 = dataDir + "Calibri"
fontFile = dataDir + "arial.ttf"
# Setting first font folder with SetFontFolder method
# Second parameter directs the API to search the subfolders for font files
FontConfigs.set_font_folder(fontFolder1, True)
# Setting both font folders with SetFontFolders method
# Second parameter prohibits the API to search the subfolders for font files
FontConfigs.set_font_folders([fontFolder1, fontFolder2], False)
# Defining FolderFontSource
sourceFolder = FolderFontSource(fontFolder1, False)
# Defining FileFontSource
sourceFile = FileFontSource(fontFile)
# Defining MemoryFontSource
sourceMemory = MemoryFontSource(open(fontFile, "rb").read())
# Setting font sources
FontConfigs.set_font_sources([sourceFolder, sourceFile, sourceMemory])

字体替换机制

Aspose.Cells for Python via .NET API 还提供设置替代字体的功能。这在所需字体缺失时非常有用,用户可以提供一个字体名称列表作为替代。为实现此目的,API 提供 FontConfigs.set_font_substitutes 方法,接受两个参数。第一个参数是字符串类型,应为需要替代的字体名称。第二个参数是字符串数组,用户可以提供一组替代原字体的字体名称。

这里是一个简单的使用场景。

from aspose.cells import FontConfigs
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Substituting the Arial font with Times New Roman & Calibri
FontConfigs.set_font_substitutes("Arial", ["Times New Roman", "Calibri" ])

信息收集

除了上述方法,Aspose.Cells for Python via .NET 还提供了收集已设置的字体源和替代方案信息的方法。

  1. FontConfigs.get_font_sources 方法返回一个 FontSourceBase 类型的数组,其中包含指定字体源的列表。 如果没有设置源,则 FontConfigs.get_font_sources 方法将返回一个空数组。
  2. FontConfigs.get_font_substitutes 方法接受一个 string 类型的参数,允许指定已设置替代字体的字体名称。 如果为指定的字体名称设置了替换,则 FontConfigs.get_font_substitutes 方法将返回 null。

高级主题