書式設定を適用する
テーブルの各要素には、異なる書式設定を適用できます。たとえば、表の書式設定は表全体に適用され、行の書式設定は特定の行のみに、セルの書式設定は特定のセルのみに適用されます。
Aspose.Words は、テーブルの書式設定を取得して適用するための豊富な API を提供します。 Table、RowFormat、および CellFormat ノードを使用して書式設定を設定できます。
この記事では、さまざまなテーブル ノードに書式設定を適用する方法と、Aspose.Words がサポートするテーブルの書式設定について説明します。
別のノードにフォーマットを適用する
このセクションでは、さまざまなテーブル ノードに書式設定を適用する方法を見ていきます。
テーブルレベルの書式設定
テーブルに書式設定を適用するには、Table、PreferredWidth、および TableCollection クラスを使用して、対応する Table ノードで使用可能なプロパティを使用できます。
以下の図は、Microsoft Word での Table フォーマット機能と、Aspose.Words での対応するプロパティの表現を示しています。
次のコード例は、表にアウトラインの境界線を適用する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Align the table to the center of the page. | |
table.alignment = aw.tables.TableAlignment.CENTER | |
# Clear any existing borders from the table. | |
table.clear_borders() | |
# Set a green border around the table but not inside. | |
table.set_border(aw.BorderType.LEFT, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True) | |
table.set_border(aw.BorderType.RIGHT, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True) | |
table.set_border(aw.BorderType.TOP, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True) | |
table.set_border(aw.BorderType.BOTTOM, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True) | |
# Fill the cells with a light green solid color. | |
table.set_shading(aw.TextureIndex.TEXTURE_SOLID, drawing.Color.light_green, drawing.Color.empty()) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.apply_outline_border.docx") |
次のコード例は、すべての枠線を有効にしたテーブル (グリッド) を作成する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Clear any existing borders from the table. | |
table.clear_borders() | |
# Set a green border around and inside the table. | |
table.set_borders(aw.LineStyle.SINGLE, 1.5, drawing.Color.green) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.build_table_with_borders.docx") |
行レベルの書式設定
行レベル フォーマットは、Row、RowFormat、および RowCollection クラスを使用して制御できます。
以下の図は、Microsoft Word での Row フォーマット機能と、Aspose.Words での対応するプロパティの表現を示しています。
次のコード例は、テーブルの行の書式設定を変更する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Retrieve the first row in the table. | |
first_row = table.first_row | |
first_row.row_format.borders.line_style = aw.LineStyle.NONE | |
first_row.row_format.height_rule = aw.HeightRule.AUTO | |
first_row.row_format.allow_break_across_pages = True |
セルレベルの書式設定
セルレベルの書式設定は、Cell、CellFormat、および CellCollection クラスによって制御されます。
Cell は Row の子ノードのみになれることに注意してください。同時に、書式設定を適用できるように、Cell 内に少なくとも 1 つの Paragraph が存在する必要があります。
Paragraph に加えて、Table を Cell に挿入することもできます。
以下の図は、Microsoft Word での Cell フォーマット機能と、Aspose.Words での対応するプロパティの表現を示しています。
次のコード例は、表のセルの書式設定を変更する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
first_cell = table.first_row.first_cell | |
first_cell.cell_format.width = 30 | |
first_cell.cell_format.orientation = aw.TextOrientation.DOWNWARD | |
first_cell.cell_format.shading.foreground_pattern_color = drawing.Color.light_green |
次のコード例は、セルの内容の左/上/右/下に追加するスペースの量 (ポイント単位) を設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.start_table() | |
builder.insert_cell() | |
# Sets the amount of space (in points) to add to the left/top/right/bottom of the cell's contents. | |
builder.cell_format.set_paddings(30, 50, 30, 50) | |
builder.writeln("I'm a wonderful formatted cell.") | |
builder.end_row() | |
builder.end_table() | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.cell_padding.docx") |
行の高さを指定する
行の高さを設定する最も簡単な方法は、DocumentBuilder を使用することです。適切な RowFormat プロパティを使用すると、デフォルトの高さ設定を設定したり、テーブル内の各行に異なる高さを適用したりできます。
Aspose.Words では、テーブルの行の高さは次によって制御されます。
- 行の高さのプロパティ – Height
- 指定された行の高さルールのプロパティ – HeightRule
同時に、各行に異なる高さを設定できるため、テーブル設定を幅広く制御できます。
次のコード例は、単一のセルを含むテーブルを作成し、行の書式設定を適用する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
table = builder.start_table() | |
builder.insert_cell() | |
row_format = builder.row_format | |
row_format.height = 100 | |
row_format.height_rule = aw.HeightRule.EXACTLY | |
# These formatting properties are set on the table and are applied to all rows in the table. | |
table.left_padding = 30 | |
table.right_padding = 30 | |
table.top_padding = 30 | |
table.bottom_padding = 30 | |
builder.writeln("I'm a wonderful formatted row.") | |
builder.end_row() | |
builder.end_table() | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.apply_row_formatting.docx") |
テーブルとセルの幅の指定
Microsoft Word ドキュメントのテーブルには、テーブルと個々のセルのサイズを変更するためのいくつかの異なる方法が用意されています。これらのプロパティを使用すると、テーブルの外観と動作を大幅に制御できるため、Aspose.Words は Microsoft Word と同様にテーブルの動作をサポートします。
テーブル要素には、個々のセルだけでなくテーブル全体の幅の計算方法に影響を与える可能性があるいくつかの異なるプロパティがあることを理解しておくことが重要です。
- テーブル上の好みの幅
- 個々のセルの推奨幅
- テーブル上での自動調整を許可する
この記事では、さまざまなテーブル幅計算プロパティの仕組みと、テーブル幅計算を完全に制御する方法について詳しく説明します。これは テーブル レイアウトが期待どおりに表示されない場合に知っておくと特に役立ちます。
ほとんどの場合、テーブル幅よりも優先セルの方が推奨されます。推奨されるセル幅は、Aspose.Words モデルだけでなく DOCX フォーマット仕様にもより準拠しています。
セル幅は実際には DOCX 形式の計算値です。実際のセル幅はさまざまな要因によって決まります。たとえば、ページの余白や優先される表の幅を変更すると、実際のセルの幅に影響を与える可能性があります。
推奨されるセル幅は、ドキュメントに保存されているセルのプロパティです。これは何にも依存せず、テーブルやセルの他のプロパティを変更しても変わりません。
優先幅の使用方法
テーブルまたは個々のセルの希望の幅は、要素が適合しようとするサイズである優先幅プロパティを通じて定義されます。つまり、テーブル全体または個々のセルに対して優先幅を指定できます。状況によっては、この幅を正確に合わせることができない場合がありますが、ほとんどの場合、実際の幅はこの値に近くなります。
適切な優先幅のタイプと値は、PreferredWidth クラスのメソッドを使用して設定されます。
- 自動または「優先幅なし」を指定する Auto メソッド
- パーセント幅を指定する FromPercent メソッド
- 幅をポイント単位で指定する FromPoints メソッド
以下の図は、Microsoft Word の 優先幅設定機能 と、Aspose.Words の対応するプロパティを示しています。
これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例を以下の図に示します。
優先するテーブルまたはセルの幅を指定する
Aspose.Words では、テーブルとセルの幅は Table.PreferredWidth プロパティと CellFormat.PreferredWidth プロパティを使用して設定されます。PreferredWidthType 列挙で使用できるオプションは次のとおりです。
- Auto、優先幅が設定されていないことと同等
- Percent。ウィンドウ内の利用可能なスペースまたはコンテナのサイズに相対して要素を適合させ、利用可能な幅が変化したときに値を再計算します。
- Points、ポイント単位で指定された幅の要素に対応します
Table.PreferredWidth プロパティを使用すると、そのコンテナー (ネストされたテーブルの場合はページ、テキスト列、または外部テーブルのセル) に応じて優先幅が調整されます。
次のコード例は、表をページ幅の 50% に自動調整するように設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Autofit the first table to the page width. | |
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_window.docx") |
特定のセルで CellFormat.PreferredWidth プロパティを使用すると、そのセルの優先幅が調整されます。
次のコード例は、さまざまな優先幅設定を設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
# Insert a table row made up of three cells which have different preferred widths. | |
builder.start_table() | |
# Insert an absolute sized cell. | |
builder.insert_cell() | |
builder.cell_format.preferred_width = aw.tables.PreferredWidth.from_points(40) | |
builder.cell_format.shading.background_pattern_color = drawing.Color.light_yellow | |
builder.writeln("Cell at 40 points width") | |
# Insert a relative (percent) sized cell. | |
builder.insert_cell() | |
builder.cell_format.preferred_width = aw.tables.PreferredWidth.from_percent(20) | |
builder.cell_format.shading.background_pattern_color = drawing.Color.light_blue | |
builder.writeln("Cell at 20% width") | |
# Insert a auto sized cell. | |
builder.insert_cell() | |
builder.cell_format.preferred_width = aw.tables.PreferredWidth.AUTO | |
builder.cell_format.shading.background_pattern_color = drawing.Color.light_green | |
builder.writeln( | |
"Cell automatically sized. The size of this cell is calculated from the table preferred width.") | |
builder.writeln("In this case the cell will fill up the rest of the available space.") | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.preferred_width_settings.docx") |
推奨される幅のタイプと値を見つける
Type プロパティと Value プロパティを使用して、目的のテーブルまたはセルの適切な幅の詳細を見つけることができます。
次のコード例は、表のセルの推奨幅タイプを取得する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
table.allow_auto_fit = True | |
first_cell = table.first_row.first_cell | |
type_ = first_cell.cell_format.preferred_width.type | |
value = first_cell.cell_format.preferred_width.value |
自動調整の設定方法
AllowAutoFit プロパティを使用すると、選択した基準に従ってテーブル内のセルを拡大および縮小できます。たとえば、ウィンドウに自動フィット オプションを使用して表をページの幅に合わせたり、コンテンツに自動フィット オプションを使用して各セルを内容に応じて拡大または縮小したりできます。
デフォルトでは、Aspose.Words は ウィンドウに自動フィット を使用して新しいテーブルを挿入します。テーブルのサイズは、使用可能なページ幅に応じて調整されます。テーブルのサイズを変更するには、AutoFit メソッドを呼び出すことができます。このメソッドは、テーブルに適用される自動調整のタイプを指定する AutoFitBehavior 列挙を受け入れます。
autofit メソッドは実際には、テーブルにさまざまなプロパティを同時に適用するショートカットであることを知っておくことが重要です。これらは、観察された動作をテーブルに実際に与えるプロパティです。各自動調整オプションのこれらのプロパティについて説明します。
次のコード例は、内容に応じて各セルを縮小または拡大するようにテーブルを設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
table.allow_auto_fit = True |
テーブルをウィンドウに自動調整
ウィンドウへの自動調整がテーブルに適用されると、実際には次の操作がバックグラウンドで実行されます。
- Table.AllowAutoFit プロパティが有効になり、Table.PreferredWidth 値 100% を使用して、利用可能なコンテンツに合わせて列のサイズを自動的に変更します。
- 表のすべてのセルから CellFormat.PreferredWidth が削除されます
これは、各セルの優先幅が現在のサイズと内容に基づいて適切な値に設定される Microsoft Word の動作とは若干異なることに注意してください。 Aspose.Words は優先幅を更新しないため、代わりにクリアされるだけです。
- 現在のテーブルの内容に合わせて列幅が再計算されます。最終結果は、使用可能な幅全体を占めるテーブルになります。
- ユーザーがテキストを編集すると、テーブルの列の幅が自動的に変わります。
次のコード例は、表をページ幅に自動調整する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Autofit the first table to the page width. | |
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_window.docx") |
テーブルをコンテンツに自動調整
テーブルの内容が自動調整されると、実際には次の手順がバックグラウンドで実行されます。
-
Table.AllowAutoFit プロパティが有効になり、内容に応じて各セルのサイズが自動的に変更されます。
-
優先テーブル幅が Table.PreferredWidth から削除され、CellFormat.PreferredWidth が各テーブル セルから削除されます。
この自動調整オプションは、Microsoft Word と同様に、セルから推奨幅を削除することに注意してください。列のサイズを維持し、コンテンツに合わせて列を増減したい場合は、自動調整ショートカットを使用するのではなく、独自に Table.AllowAutoFit プロパティを True に設定する必要があります。 -
現在のテーブルの内容に合わせて列の幅が再計算されます。最終的には、ユーザーがテキストを編集すると、列の幅とテーブル全体の幅がコンテンツに最適になるように自動的にサイズ変更されるテーブルが作成されます。
次のコード例は、テーブルをそのコンテンツに自動調整する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_CONTENTS) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_contents.docx") |
表の自動調整を無効にし、固定列幅を使用する
テーブルの自動調整が無効になっており、代わりに固定列幅が使用されている場合は、次の手順が実行されます。
- Table.AllowAutoFit プロパティが無効になっているため、列は内容に合わせて拡大または縮小されません。
- テーブル全体の推奨幅が Table.PreferredWidth から削除され、CellFormat.PreferredWidth がすべてのテーブル セルから削除されます。
- 最終結果は、列幅が CellFormat.Width プロパティによって決定され、ユーザーがテキストを入力したときやページのサイズが変更されたときに列のサイズが自動的に変更されない表になります。
次のコード例は、指定したテーブルの自動調整を無効にし、固定幅を有効にする方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Disable autofitting on this table. | |
table.auto_fit(aw.tables.AutoFitBehavior.FIXED_COLUMN_WIDTHS) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_fixed_column_widths.docx") |
セル幅を計算する際の優先順位
Aspose.Words を使用すると、ユーザーは CellFormat を含む複数のオブジェクトを通じてテーブルまたはセルの幅を定義できます。その Width プロパティはほとんどが以前のバージョンから残されていますが、セル幅の設定を簡素化するのに依然として役立ちます。
CellFormat.Width プロパティの動作は、テーブル内に既に存在する他の幅プロパティに応じて異なることを理解しておくことが重要です。
Aspose.Words は、セル幅の計算に次の順序を使用します。
注文 | 財産 | 説明 |
---|---|---|
1 | AllowAutoFitが決定 | AutoFit が有効な場合: - テーブルは、コンテンツを収容するために優先幅を超えて拡大する場合があります。通常、優先幅よりも縮小することはありません - CellFormat.Width 値への変更は無視され、セルはその内容に合わせて表示されます。 |
2 | Points または Percent の値を持つ PreferredWidthType | CellFormat.Widthは無視されます |
3 | 値がAutoのPreferredWidthType | CellFormat.Width からの値がコピーされ、セルの優先幅 (ポイント単位) になります。 |
セル間の間隔を許可する
Microsoft Word の「セル間隔」オプションと同様に、表のセル間の追加のスペースを取得または設定できます。これは、AllowCellSpacing プロパティを使用して実行できます。
これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例を以下の図に示します。

次のコード例は、セル間の間隔を設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
table.allow_cell_spacing = True | |
table.cell_spacing = 2 | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.allow_cell_spacing.docx") |
境界線とシェーディングの適用
枠線とシェーディングは、Table.SetBorder、Table.SetBorders、Table.SetShading を使用してテーブル全体に適用することも、CellFormat.Borders と CellFormat.Shading を使用して特定のセルにのみ適用することもできます。さらに、RowFormat.Borders を使用して行の境界線を設定できますが、この方法ではシェーディングを適用できません。
以下の図は、Microsoft Word の境界線と影の設定と、Aspose.Words の対応するプロパティを示しています。
次のコード例は、異なる枠線と網掛けを使用してテーブルとセルを書式設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
table = builder.start_table() | |
builder.insert_cell() | |
# Set the borders for the entire table. | |
table.set_borders(aw.LineStyle.SINGLE, 2.0, drawing.Color.black) | |
# Set the cell shading for this cell. | |
builder.cell_format.shading.background_pattern_color = drawing.Color.red | |
builder.writeln("Cell #1") | |
builder.insert_cell() | |
# Specify a different cell shading for the second cell. | |
builder.cell_format.shading.background_pattern_color = drawing.Color.green | |
builder.writeln("Cell #2") | |
builder.end_row() | |
# Clear the cell formatting from previous operations. | |
builder.cell_format.clear_formatting() | |
builder.insert_cell() | |
# Create larger borders for the first cell of this row. This will be different | |
# compared to the borders set for the table. | |
builder.cell_format.borders.left.line_width = 4.0 | |
builder.cell_format.borders.right.line_width = 4.0 | |
builder.cell_format.borders.top.line_width = 4.0 | |
builder.cell_format.borders.bottom.line_width = 4.0 | |
builder.writeln("Cell #3") | |
builder.insert_cell() | |
builder.cell_format.clear_formatting() | |
builder.writeln("Cell #4") | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.format_table_and_cell_with_different_borders.docx") |