フォント設定
フォント設定の構成
Aspose.Cells for Python via .NETは、Microsoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるworksheetsコレクションが含まれています。ワークシートは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プロパティを使用してフォントの色を設定します。.NETフレームワークの一部であるColor列挙型から任意の色を選択して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列挙体にさまざまな事前定義されたフォント下線タイプを提供します。
フォントの下線の種類 | 説明 |
---|---|
会計 | 単一の会計下線 |
二重 | 二重下線 |
二重会計 | 二重会計下線 |
なし | 下線なし |
シングル | 単一下線 |
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") |