行と列のフォーマット
行で操作する
行の高さの調整方法
Aspose.Cells for Python via .NETでは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスには、ワークシート内のすべてのセルを表すCellsコレクションが提供されます。
Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが用意されています。以下では、そのうちいくつかを詳しく説明します。
行の高さを設定する方法
行の高さを単一の行に設定することができます。Cellsコレクションのset_row_heightメソッドを呼び出すことで行の高さを設定できます。set_row_heightメソッドは以下のパラメータを取ります。
- row、行のインデックス、その行の高さを変更します。
- height、行に適用する行の高さ。
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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Setting the height of the second row to 13 | |
worksheet.cells.set_row_height(1, 13) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
ワークシート内のすべての行の高さを設定する方法
開発者がワークシート内のすべての行に同じ行の高さを設定する必要がある場合、Cellsコレクションのstandard_heightプロパティを使用して行の高さを設定できます。
例:
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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Setting the height of all rows in the worksheet to 15 | |
worksheet.cells.standard_height = 15.0 | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
列で操作する
列の幅を設定する方法
列の幅を変更する列のインデックスを指定して、Cellsコレクションのset_column_widthメソッドを呼び出すことで列の幅を設定する。set_column_widthメソッドは次のパラメータを取ります:
- column: 幅を変更する列のインデックス。
- width: 望ましい列の幅。
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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Setting the width of the second column to 17.5 | |
worksheet.cells.set_column_width(1, 17.5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
ピクセル単位で列幅を設定する方法
列の幅を変更する列のインデックスを指定して、Cellsコレクションのset_column_width_pixelメソッドを呼び出すことで列の幅を設定する。set_column_width_pixelメソッドは次のパラメータを取ります:
- column: 幅を変更する列のインデックス。
- pixels: ピクセル単位での望ましい列の幅。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
outDir = RunExamples.Get_OutputDirectory() | |
# Load source Excel file | |
workbook = Workbook(sourceDir + "Book1.xlsx") | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Set the width of the column in pixels | |
worksheet.cells.set_column_width_pixel(7, 200) | |
workbook.save(outDir + "SetColumnWidthInPixels_Out.xlsx") |
ワークシート内のすべての列の幅を設定する方法
ワークシート内のすべての列に同じ列の幅を設定するには、Cellsコレクションのstandard_widthプロパティを使用します。
from aspose.cells import 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) | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Setting the width of all columns in the worksheet to 20.5 | |
worksheet.cells.standard_width = 20.5 | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |