塗りつぶし設定

色と背景パターン

Microsoft Excel では、セルの前景(輪郭)と背景(塗りつぶし)の色、および背景パターンを設定できます。

Aspose.Cells for Python via .NETは、これらの機能も柔軟にサポートしています。このトピックでは、Aspose.Cellsを使用してこれらの機能を使う方法を学びます。

色と背景パターンの設定

Aspose.Cells for Python via .NETは、Microsoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるworksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。WorksheetクラスはCellsコレクションを提供します。Cellsコレクションの各アイテムはCellクラスのオブジェクトを表します。

Cellはセルの書式設定を取得・設定するためのget_styleset_styleメソッドを持っています。Styleクラスはセルの前景色と背景色を設定するプロパティを提供します。Aspose.Cells for Python via .NETは、以下に示す背景パターンの事前定義されたタイプを含むBackgroundType列挙体を提供します。

背景パターン 説明
DIAGONAL_CROSSHATCH 斜交格子パターンを表します
DIAGONAL_STRIPE 斜線ストライプパターンを表します
GRAY6 6.25%のグレーパターンを表します
GRAY12 12.5%のグレーパターンを表します
GRAY25 25%のグレーパターンを表します
GRAY50 50%のグレーパターンを表します
GRAY75 75%のグレーパターンを表します
HORIZONTAL_STRIPE 水平ストライプパターンを表します
NONE 背景なしを表します
REVERSE_DIAGONAL_STRIPE 逆斜線ストライプパターンを表します
SOLID 単色パターンを表します
THICK_DIAGONAL_CROSSHATCH 太い斜交格子パターンを表します
THIN_DIAGONAL_CROSSHATCH 細い斜交格子パターンを表します
THIN_DIAGONAL_STRIPE 細い斜線ストライプパターンを表します
THIN_HORIZONTAL_CROSSHATCH 細い横格子パターンを表します
THIN_HORIZONTAL_STRIPE 細い横ストライプパターンを表します
THIN_REVERSE_DIAGONAL_STRIPE 細い逆斜線ストライプパターンを表します
THIN_VERTICAL_STRIPE 細い縦ストライプパターンを表します
VERTICAL_STRIPE 縦ストライプパターンを表します

以下の例では、A1セルの前景色が設定されていますが、A2は前景色と背景色の両方を垂直ストライプの背景パターンで構成するように設定されています。

from aspose.cells import BackgroundType, 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 a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Define a Style and get the A1 cell style
style = worksheet.cells.get("A1").get_style()
# Setting the foreground color to yellow
style.foreground_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A1 cell
worksheet.cells.get("A1").set_style(style)
# Get the A2 cell style
style = worksheet.cells.get("A2").get_style()
# Setting the foreground color to blue
style.foreground_color = Color.blue
# Setting the background color to yellow
style.background_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A2 cell
worksheet.cells.get("A2").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

重要なこと

グラデーション塗りつぶし効果の適用

セルに希望のグラデーション塗りつぶし効果を適用するには、Style オブジェクトの set_two_color_gradient メソッドを使用してください。

from aspose.cells import TextAlignmentType, Workbook
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
# 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(".")
# Instantiate a new Workbook
workbook = Workbook()
# Get the first worksheet (default) in the workbook
worksheet = workbook.worksheets[0]
# Input a value into B3 cell
worksheet.cells.get(2, 1).put_value("test")
# Get the Style of the cell
style = worksheet.cells.get("B3").get_style()
# Set Gradient pattern on
style.is_gradient = True
# Specify two color gradient fill effects
style.set_two_color_gradient(Color.from_argb(255, 255, 255), Color.from_argb(79, 129, 189), GradientStyleType.HORIZONTAL, 1)
# Set the color of the text in the cell
style.font.color = Color.red
# Specify horizontal and vertical alignment settings
style.horizontal_alignment = TextAlignmentType.CENTER
style.vertical_alignment = TextAlignmentType.CENTER
# Apply the style to the cell
worksheet.cells.get("B3").set_style(style)
# Set the third row height in pixels
worksheet.cells.set_row_height_pixel(2, 53)
# Merge the range of cells (B3:C3)
worksheet.cells.merge(2, 1, 1, 2)
# Save the Excel file
workbook.save(dataDir + "output.xlsx")

色とパレット

パレットとは、画像を作成するために使用可能な色の数です。プレゼンテーションで標準化されたパレットを使用することで、ユーザーは一貫した外観を作成できます。各 Microsoft Excel (97-2003) ファイルには、セル、フォント、グリッド線、グラフィックオブジェクト、塗りつぶし、およびグラフの線に適用できる 56 色のパレットがあります。

Aspose.Cells for Python via .NETを使用すれば、既存のパレットの色だけでなく、カスタムカラーも使用できます。カスタムカラーを使う前に、最初にパレットに追加してください。

このトピックでは、パレットにカスタム色を追加する方法について説明します。

パレットにカスタム色を追加

Aspose.Cells for Python via .NETは、Microsoft Excelの56色パレットをサポートしています。パレットに定義されていないカスタムカラーを使用するには、その色をパレットに追加してください。

Aspose.Cells for Python via .NETは、Microsoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、次のパラメータを受け取ってパレットを変更するカスタムカラーを追加するためのchange_paletteメソッドがあります:

  • カスタムカラー、追加するカスタムカラー。 カスタムカラーが置き換えるパレット内の色のインデックスです。0〜55の間である必要があります。

以下の例では、カスタムカラー(Orchid)をパレットに追加し、フォントに適用する前に追加します。

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)