テーブルスタイルを適用

表スタイルは、表に簡単に適用できる一連の書式設定を定義します。枠線、網掛け、配置、フォントなどの書式設定を表スタイルで設定し、多くの表に適用して一貫した外観を実現できます。

Aspose.Words は、テーブル スタイルのテーブルへの適用と、任意のテーブル スタイルのプロパティの読み取りをサポートします。表のスタイルは、ロード中および保存中に次の方法で保持されます。

  • DOCX および WordML 形式の表スタイルは、これらの形式にロードおよび保存するときに保持されます。
  • DOC 形式でロードおよび保存する場合、テーブル スタイルは保持されます (ただし、他の形式では保持されません)。
  • 他の形式にエクスポートする場合、レンダリングまたは印刷する場合、表スタイルは表内の直接書式設定に拡張されるため、すべての書式設定が保持されます。

表スタイルを作成する

ユーザーは新しいスタイルを作成し、スタイル コレクションに追加できます。 Add メソッドは、新しいテーブル スタイルを作成するために使用されます。

次のコード例は、新しいユーザー定義のテーブル スタイルを作成する方法を示しています。

# 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()
builder.write("Name")
builder.insert_cell()
builder.write("Value")
builder.end_row()
builder.insert_cell()
builder.insert_cell()
builder.end_table()
table_style = doc.styles.add(aw.StyleType.TABLE, "MyTableStyle1").as_table_style()
table_style.borders.line_style = aw.LineStyle.DOUBLE
table_style.borders.line_width = 1
table_style.left_padding = 18
table_style.right_padding = 18
table_style.top_padding = 12
table_style.bottom_padding = 12
table.style = table_style
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.create_table_style.docx")

既存の表スタイルをコピーする

必要に応じて、AddCopy メソッドを使用して、特定のドキュメントに既に存在する表スタイルをスタイル コレクションにコピーできます。

このコピーでは、リンクされたスタイルもコピーされることに注意してください。

次のコード例は、あるドキュメントから別のドキュメントにスタイルをインポートする方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
src_doc = aw.Document()
# Create a custom style for the source document.
src_style = src_doc.styles.add(aw.StyleType.PARAGRAPH, "MyStyle")
src_style.font.color = drawing.Color.red
# Import the source document's custom style into the destination document.
dst_doc = aw.Document()
new_style = dst_doc.styles.add_copy(src_style)
# The imported style has an appearance identical to its source style.
self.assertEqual("MyStyle", new_style.name)
self.assertEqual(drawing.Color.red.to_argb(), new_style.font.color.to_argb())

既存の表スタイルを適用する

Aspose.Words は、Style クラスから継承された TableStyle を提供します。 TableStyle を使用すると、ユーザーはシェーディング、パディング、インデント、CellSpacingFont などのさまざまなスタイル オプションを簡単に適用できます。

さらに、Aspose.Words は、StyleStyleIdentifierStyleNameStyleOptions のどのテーブル スタイルを使用するかを指定するための StyleCollection クラスと Table クラスのいくつかのプロパティを提供します。

Aspose.Words は、テーブル スタイルが割り当てられたテーブルの一部の領域に適用される特別な書式設定を表す ConditionalStyle クラスと、ConditionalStyle オブジェクトのコレクションを表す ConditionalStyleCollection も提供します。このコレクションには、ConditionalStyleType 列挙型の値ごとに 1 つの項目を表す項目の永続的なセットが含まれています。 ConditionalStyleType 列挙は、表スタイルで条件付き書式を定義できるすべての可能な表領域を定義します。

この場合、条件付き書式設定は、ConditionalStyleType 列挙型で定義されたすべての可能な表領域に対して定義できます。

次のコード例は、テーブルのヘッダー行の条件付き書式設定を定義する方法を示しています。

# 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()
builder.write("Name")
builder.insert_cell()
builder.write("Value")
builder.end_row()
builder.insert_cell()
builder.insert_cell()
builder.end_table()
table_style = doc.styles.add(aw.StyleType.TABLE, "MyTableStyle1").as_table_style()
table_style.conditional_styles.first_row.shading.background_pattern_color = drawing.Color.green_yellow
table_style.conditional_styles.first_row.shading.texture = aw.TextureIndex.TEXTURE_NONE
table.style = table_style
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.define_conditional_formatting.docx")

最初の列、最後の列、帯状の行など、スタイルを適用する表の部分を選択することもできます。これらは TableStyleOptions 列挙にリストされ、StyleOptions プロパティを通じて適用されます。 TableStyleOptions 列挙では、これらの機能をビットごとに組み合わせることができます。

次のコード例は、テーブル スタイルが適用された新しいテーブルを作成する方法を示しています。

# 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()
# We must insert at least one row first before setting any table formatting.
builder.insert_cell()
# Set the table style used based on the unique style identifier.
table.style_identifier = aw.StyleIdentifier.MEDIUM_SHADING1_ACCENT1
# Apply which features should be formatted by the style.
table.style_options = aw.tables.TableStyleOptions.FIRST_COLUMN | aw.tables.TableStyleOptions.ROW_BANDS | aw.tables.TableStyleOptions.FIRST_ROW
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_CONTENTS)
builder.writeln("Item")
builder.cell_format.right_padding = 40
builder.insert_cell()
builder.writeln("Quantity (kg)")
builder.end_row()
builder.insert_cell()
builder.writeln("Apples")
builder.insert_cell()
builder.writeln("20")
builder.end_row()
builder.insert_cell()
builder.writeln("Bananas")
builder.insert_cell()
builder.writeln("40")
builder.end_row()
builder.insert_cell()
builder.writeln("Carrots")
builder.insert_cell()
builder.writeln("50")
builder.end_row()
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.build_table_with_style.docx")

以下の図は、Microsoft Word での Table Styles の表現と、Aspose.Words での対応するプロパティを示しています。

formatting-table-style-aspose-words-python

テーブルスタイルの操作

表スタイルは、表に簡単に適用できる一連の書式設定を定義します。枠線、網掛け、配置、フォントなどの書式設定を表スタイルで設定し、多くの表に適用して一貫した外観を実現できます。

Aspose.Words は、テーブル スタイルのテーブルへの適用と、任意のテーブル スタイルのプロパティの読み取りをサポートします。表のスタイルは、ロードおよび保存中に次の方法で保持されます。

  • DOCX および WordML 形式の表スタイルは、これらの形式にロードおよび保存するときに保持されます。
  • 表のスタイルは、DOC 形式でロードおよび保存するときに保持されます (ただし、他の形式では保持されません)。
  • 他の形式にエクスポートする場合、レンダリングまたは印刷する場合、テーブル スタイルはテーブル上の直接書式設定に拡張されるため、すべての書式設定が保持されます。

現在、新しいテーブル スタイルを作成することはできません。表に適用できるのは、組み込みの表スタイルまたはドキュメント内にすでに存在するカスタム表スタイルのみです。

テーブル スタイルから書式設定を取得し、直接書式設定として適用します

Aspose.Words は、テーブル スタイルで見つかった書式設定を取得し、それを直接書式設定としてテーブルの行とセルに展開する ExpandTableStylesToDirectFormatting メソッドも提供します。書式設定とテーブル スタイルおよびセル スタイルを組み合わせてみてください。

次のコード例は、スタイルの書式設定を直接書式設定としてテーブルの行とセルに拡張する方法を示しています。

# 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")
# Get the first cell of the first table in the document.
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
first_cell = table.first_row.first_cell
# First print the color of the cell shading.
# This should be empty as the current shading is stored in the table style.
cell_shading_before = first_cell.cell_format.shading.background_pattern_color
print("Cell shading before style expansion:", cell_shading_before)
doc.expand_table_styles_to_direct_formatting()
# Now print the cell shading after expanding table styles.
# A blue background pattern color should have been applied from the table style.
cell_shading_after = first_cell.cell_format.shading.background_pattern_color
print("Cell shading after style expansion:", cell_shading_after)