访问和更新单元格的富文本部分
Contents
[
Hide
]
Aspose.Cells for Python via .NET允许您访问和更新单元格的富文本部分。为此,您可以使用Cell.get_characters()和Cell.set_characters()方法。这些方法将返回和接受FontSetting对象的数组,您可以使用这些对象来访问和更新字体的各种属性,例如字体名称、字体颜色、加粗等。
访问和更新单元格的富文本部分
以下代码演示了如何使用source excel file中的Cell.get_characters()和Cell.set_characters()方法。您可以从提供的链接下载源Excel文件。源Excel文件中的单元格A1中有富文本。它有3个部分,每个部分都有不同的字体。以下代码段访问这些部分,并使用新的字体名称更新第一个部分。最后,它将工作簿另存为output excel file。当您打开它时,您会发现文本的第一部分的字体已经更改为**“Arial”**。
###访问和更新单元格的富文本部分的C#代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
# 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(".") | |
inputPath = dataDir + "Sample.xlsx" | |
outputPath = dataDir + "Output.out.xlsx" | |
workbook = Workbook(inputPath) | |
worksheet = workbook.worksheets[0] | |
cell = worksheet.cells.get("A1") | |
print("Before updating the font settings....") | |
fnts = cell.get_characters() | |
for i in range(len(fnts)): | |
print(fnts[i].font.name) | |
# Modify the first FontSetting Font Name | |
fnts[0].font.name = "Arial" | |
# And update it using SetCharacters() method | |
cell.set_characters(fnts) | |
print() | |
print("After updating the font settings....") | |
fnts = cell.get_characters() | |
for i in range(len(fnts)): | |
print(fnts[i].font.name) | |
# Save workbook | |
workbook.save(outputPath) |
样本代码生成的控制台输出
以下是使用source excel file的上述示例代码的控制台输出。
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana