如何使用颜色调色板

颜色和调色板

调色板是在创建图像时可用的颜色数量。在演示文稿中使用标准调色板可以让用户创建一致的外观。每个Microsoft Excel(97-2003)文件都有一个包含可应用于单元格、字体、网格线、图形对象、填充和图表中的线条的56种颜色的调色板。

使用 Aspose.Cells for Python via .NET,不仅可以使用调色板中的现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,首先将其添加到调色板中。

本主题讨论如何向调色板中添加自定义颜色。

向调色板添加自定义颜色

Aspose.Cells for Python via .NET 支持微软 Excel 的 56 种调色板。若要使用调色板中未定义的自定义颜色,请将其添加到调色板中。

Aspose.Cells for Python via .NET 提供了一个类 Workbook,表示微软 Excel 文件。Workbook 类提供了一个 change_palette 方法,带有以下参数,用于添加自定义颜色以修改调色板:

  • Custom Color,要添加的自定义颜色。
  • Index,自定义颜色在调色板中的索引,将替换指定的颜色。应该在 0-55 之间。

下面的示例在应用于字体之前向调色板中添加了自定义颜色(兰花紫)。

from aspose.cells import SaveFormat, Workbook
from aspose.pydrawing import Color
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 an Workbook object
workbook = Workbook()
# Adding Orchid color to the palette at 55th index
workbook.change_palette(Color.orchid, 55)
# 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!")
# Defining new Style object
styleObject = workbook.create_style()
# Setting the Orchid (custom) color to the font
styleObject.font.color = Color.orchid
# Applying the style to the cell
cell.set_style(styleObject)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.AUTO)