字体设置
配置字体设置
Aspose.Cells for Python via .NET 提供了一个类 Workbook,代表微软 Excel 文件。Workbook 类包含一个 worksheets 集合,可以访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 cells 集合。cells 集合中的每个项目表示 Cell 类的对象。
Aspose.Cells提供了Cell类的get_style和set_style方法,用于获取和设置单元格的格式样式。Style类提供了用于配置字体设置的属性。
设置字体名称
开发者可以使用 Style.font 对象的 name 属性,将任何字体应用于单元格内的文本。
from aspose.cells import SaveFormat, Workbook | |
from os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font name to "Times New Roman" | |
style.font.name = "Times New Roman" | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
将字体样式设置为粗体
开发人员通过将Style.font对象的is_bold属性设置为true来使文本加粗。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font weight to bold | |
style.font.is_bold = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save("out.xlsx") |
设置字体大小
使用Style.font对象的size属性设置字体大小。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font size to 14 | |
style.font.size = 14 | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save("out.xlsx") |
设置字体颜色
使用Style.font对象的color属性设置字体颜色。从Color枚举(.NET框架的一部分)中选择任何颜色,并将其分配给color属性。
from aspose.cells import Workbook | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font color to blue | |
style.font.color = Color.blue | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save("out.xlsx") |
设置字体下划线类型
使用 Style.font 对象的 underline 属性为文本添加下划线。Aspose.Cells for Python via .NET 提供了在 FontUnderlineType 枚举中的各种预定义字体下划线类型。
字体下划线类型 | 描述 |
---|---|
ACCOUNTING | 单一会计下划线 |
DOUBLE | 双下划线 |
DOUBLE_ACCOUNTING | 双重会计下划线 |
NONE | 无下划线 |
SINGLE | 单一下划线 |
from aspose.cells import FontUnderlineType, Workbook | |
from os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font to be underlined | |
style.font.underline = FontUnderlineType.SINGLE | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "out.xlsx") |
设置删除线效果
通过将Style.font对象的is_strikeout属性设置为true应用删除线。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the strike out effect on the font | |
style.font.is_strikeout = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "out.xlsx") |
设置下标效果
通过将Style.font对象的is_subscript属性设置为true应用下标。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting subscript effect | |
style.font.is_subscript = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "out.xlsx") |
在字体上设置上标效果
开发人员可以通过将Style.font对象的is_superscript属性设置为true在字体上应用上标效果。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting superscript effect | |
style.font.is_superscript = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "out.xlsx") |