配置設定
配置設定の構成
Microsoft Excelの配置設定
セルの書式設定にMicrosoft Excelを使用したことがある人であれば、Microsoft Excelの配置設定に精通しているでしょう。
上記の図から分かるように、異なる種類の配置オプションがあります:
- テキストの配置(水平および垂直)
- インデント
- 方向
- テキスト コントロール。
- テキスト方向。
これらの整列設定はすべてAspose.Cells for Python via .NETで完全にサポートされており、詳細は以下に記載されています。
Aspose.Cells for Python via .NETの整列設定
Aspose.Cells for Python via .NETは、Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスするためのworksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスはcellsコレクションを提供します。cellsコレクションの各アイテムはCellクラスのオブジェクトを表します。
Aspose.Cells for Python via .NETは、セルの書式を取得・設定するためのget_styleとset_styleメソッドを提供します。Cellクラスは、整列設定を構成するための便利なプロパティを提供します。
TextAlignmentType 列挙型を使用して任意のテキスト配置タイプを選択します。TextAlignmentType 列挙型の事前定義されたテキスト配置タイプは次のとおりです:
テキスト配置タイプ | 説明 |
---|---|
GENERAL | 一般的なテキスト整列を表す |
BOTTOM | 下部のテキスト整列を表す |
CENTER | 中央のテキスト整列を表す |
CENTER_ACROSS | 横に中央揃えのテキスト整列を表す |
DISTRIBUTED | 分散されたテキスト整列を表す |
FILL | 塗りつぶしのテキスト整列を表す |
JUSTIFY | 両端揃えのテキスト整列を表す |
LEFT | 左寄せのテキスト配置を表す |
RIGHT | 右寄せのテキスト配置を表す |
TOP | 上寄せのテキスト配置を表す |
JUSTIFIED_LOW | アラビア語テキストのための調整されたカシダの長さでテキストを揃える |
THAI_DISTRIBUTED | タイ語を特別に分散させる、各文字が単語として扱われるため |
水平配置
Style オブジェクトのhorizontal_alignment プロパティを使用してテキストを水平に配置します。
from aspose.cells import SaveFormat, TextAlignmentType, 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() | |
# Obtaining the reference of the worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style = cell.get_style() | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
垂直配置
水平配置と同様に、Style オブジェクトのvertical_alignment プロパティを使用してテキストを垂直に配置します。
from aspose.cells import SaveFormat, TextAlignmentType, 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() | |
# Clearing all the worksheets | |
workbook.worksheets.clear() | |
# 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("Visit Aspose!") | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style = cell.get_style() | |
# Setting the vertical alignment of the text in a cell | |
style.vertical_alignment = TextAlignmentType.CENTER | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
インデント
セル内のテキストのインデントレベルをStyle オブジェクトのindent_level プロパティで設定することができます。
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() | |
# Obtaining the reference of the worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style = cell.get_style() | |
# Setting the indentation level of the text (inside the cell) to 2 | |
style.indent_level = 2 | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
方向
セル内のテキストの方向(回転)をStyle オブジェクトのrotation_angle プロパティで設定します。
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() | |
# Obtaining the reference of the worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style = cell.get_style() | |
# Setting the rotation of the text (inside the cell) to 25 | |
style.rotation_angle = 25 | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
テキストコントロール
次のセクションでは、テキストの折り返し、収縮に合わせるなど、テキストの制御方法について説明します。
テキストの折り返し
セル内のテキストを折り返すと、テキストが切れたり隣接するセルに流れ出ないようになり、読みやすくなります。テキストの折り返しは、Styleのis_text_wrappedプロパティを使用してオンまたはオフに設定できます。
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(".") | |
# Create Workbook Object | |
wb = Workbook() | |
# Open first Worksheet in the workbook | |
ws = wb.worksheets[0] | |
# Get Worksheet Cells Collection | |
cell = ws.cells | |
# Increase the width of First Column Width | |
cell.set_column_width(0, 35) | |
# Increase the height of first row | |
cell.set_row_height(0, 36) | |
# Add Text to the Firts Cell | |
cell.get(0, 0).put_value("I am using the latest version of Aspose.Cells to test this functionality") | |
# Make Cell's Text wrap | |
style = cell.get(0, 0).get_style() | |
style.is_text_wrapped = True | |
cell.get(0, 0).set_style(style) | |
# Save Excel File | |
wb.save(dataDir + "WrappingText.out.xlsx") |
収縮に合わせる
フィールド内のテキストを折り返すオプションは、セルのサイズに合わせてテキストサイズを収縮することもできます。これは、Styleのis_text_wrappedプロパティをtrueに設定することで行います。
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() | |
# Obtaining the reference of the worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style = cell.get_style() | |
# Shrinking the text to fit according to the dimensions of the cell | |
style.shrink_to_fit = True | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
セルの結合
Microsoft Excelのように、Aspose.Cells for Python via .NETは複数のセルを一つに結合することをサポートしています。Aspose.Cells for Python via .NETはこのタスクに2つのアプローチを提供します。ひとつはcellsコレクションのmergeメソッドを呼び出す方法です。mergeメソッドは次のパラメータを取ってセルを結合します:
- 最初の行: 結合の開始行。
- 最初の列: 結合の開始列。
- 行数: 結合する行数。
- 列数: 結合する列数。
from aspose.cells import BackgroundType, 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) | |
# Create a Workbook. | |
wbk = Workbook() | |
# Create a Worksheet and get the first sheet. | |
worksheet = wbk.worksheets[0] | |
# Create a Cells object ot fetch all the cells. | |
cells = worksheet.cells | |
# Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3) | |
# Input data into C6 Cell. | |
worksheet.cells.get(5, 2).put_value("This is my value") | |
# Create a Style object to fetch the Style of C6 Cell. | |
style = worksheet.cells.get(5, 2).get_style() | |
# Create a Font object | |
font = style.font | |
# Set the name. | |
font.name = "Times New Roman" | |
# Set the font size. | |
font.size = 18 | |
# Set the font color | |
font.color = Color.blue | |
# Bold the text | |
font.is_bold = True | |
# Make it italic | |
font.is_italic = True | |
# Set the backgrond color of C6 Cell to Red | |
style.foreground_color = Color.red | |
style.pattern = BackgroundType.SOLID | |
# Apply the Style to C6 Cell. | |
cells.get(5, 2).set_style(style) | |
# Save the Workbook. | |
wbk.save(dataDir + "mergingcells.out.xls") |
もう1つの方法は、まずcellsのcreate_rangeメソッドを呼び出して結合するセルの範囲を作成する方法です。create_rangeメソッドは、前述のmergeメソッドと同じパラメータを取り、Rangeオブジェクトを返します。Rangeオブジェクトには、Rangeオブジェクトで指定された範囲を結合するmergeメソッドも用意されています。
テキストの方向
セル内のテキストの読み取り順を設定することが可能です。読み取り順は、文字や単語などが表示される視覚的な順序です。たとえば、英語は左から右への言語であり、アラビア語は右から左への言語です。
読み取り順序は、Styleオブジェクトのtext_directionプロパティで設定されます。Aspose.Cells for Python via .NETは、TextDirectionType列挙型にあらかじめ定義されたテキスト方向タイプを提供します。
テキスト方向の種類 | 説明 |
---|---|
CONTEXT | 最初に入力された文字の言語と一致した読み取り順 |
LEFT_TO_RIGHT | 左から右の読み取り順 |
RIGHT_TO_LEFT | 右から左の読み取り順 |
from aspose.cells import TextDirectionType, Workbook | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of first worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("I am using the latest version of Aspose.Cells to test this functionality.") | |
# Gets style in the "A1" cell | |
style = cell.get_style() | |
# Shrinking the text to fit according to the dimensions of the cell | |
style.text_direction = TextDirectionType.LEFT_TO_RIGHT | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save("book1.xlsx") |