列と行を操作する
テーブルの動作をより詳細に制御するには、列と行を操作する方法を学習してください。
テーブル要素インデックスの検索
列、行、セルは、インデックスによって選択したドキュメント ノードにアクセスすることによって管理されます。ノードのインデックスを検索するには、親ノードから要素タイプのすべての子ノードを収集し、IndexOf メソッドを使用してコレクション内の目的のノードのインデックスを検索します。
文書内の表の索引を検索する
場合によっては、ドキュメント内の特定の表に変更を加える必要があるかもしれません。これを行うには、インデックスによってテーブルを参照できます。
次のコード例は、ドキュメント内のテーブルのインデックスを取得する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
all_tables = doc.get_child_nodes(aw.NodeType.TABLE, True) | |
table_index = all_tables.index_of(table) |
テーブル内の行のインデックスの検索
同様に、選択したテーブルの特定の行を変更する必要がある場合があります。これを行うには、インデックスによって行を参照することもできます。
次のコード例は、テーブル内の行のインデックスを取得する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
row_index = table.index_of(table.last_row) |
行内のセルのインデックスを見つける
最後に、特定のセルに変更を加える必要がある場合がありますが、これはセル インデックスによって行うこともできます。
次のコード例は、行内のセルのインデックスを取得する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
cell_index = row.index_of(row.cells[4]) |
列の操作
Aspose.Words Document Object Model (DOM) では、Table ノードは Row ノードと Cell ノードで構成されます。したがって、Aspose.Words の Document
オブジェクト モデルには、Word ドキュメントと同様に、列の概念がありません。
設計上、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. | |
class Column: | |
"""Represents a facade object for a column of a table in a Microsoft Word document.""" | |
def __init__(self, table: aw.tables.Table, column_index: int): | |
if table is None: | |
raise ValueError("table") | |
self.table = table | |
self.column_index = column_index | |
def index_of(self, cell: aw.tables.Cell): | |
"""Returns the index of the given cell in the column.""" | |
return self.get_column_cells().index(cell) | |
def insert_column_before(self): | |
"""Inserts a brand new column before this column into the table.""" | |
column_cells = self.get_column_cells() | |
if len(column_cells) == 0: | |
raise ValueError("Column must not be empty") | |
# Create a clone of this column. | |
for cell in column_cells: | |
cell.parent_row.insert_before(cell.clone(False), cell) | |
# This is the new column. | |
column = self.__class__(column_cells[0].parent_row.parent_table, self.column_index) | |
# We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
for cell in column.get_column_cells(): | |
cell.ensure_minimum() | |
# Increase the index which this column represents since there is now one extra column in front. | |
self.column_index += 1 | |
return column | |
def remove(self): | |
"""Removes the column from the table.""" | |
for cell in self.get_column_cells(): | |
cell.remove() | |
def to_txt(self): | |
"""Returns the text of the column.""" | |
return "".join(cell.to_string(aw.SaveFormat.TEXT) for cell in self.get_column_cells()) | |
def get_column_cells(self): | |
"""Provides an up-to-date collection of cells which make up the column represented by this facade.""" | |
column_cells = [] | |
for row in self.table.rows: | |
cell = row.as_row().cells[self.column_index] | |
if cell is not None: | |
column_cells.append(cell) | |
return column_cells | |
次のコード例は、テーブルに空の列を挿入する方法を示しています。
# 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() | |
column = self.Column(table, 0) | |
# Print the plain text of the column to the screen. | |
print(column.to_txt()) | |
# Create a new column to the left of this column. | |
# This is the same as using the "Insert Column Before" command in Microsoft Word. | |
new_column = column.insert_column_before() | |
for cell in new_column.get_column_cells(): | |
cell.first_paragraph.append_child(aw.Run(doc, "Column Text " + str(new_column.index_of(cell)))) |
次のコード例は、ドキュメント内のテーブルから列を削除する方法を示しています。
# 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, 1, True).as_table() | |
column = self.Column(table, 2) | |
column.remove() |
行をヘッダー行として指定
テーブルの最初の行を最初のページでのみヘッダー行として繰り返すか、テーブルが複数に分割されている場合は各ページで繰り返すかを選択できます。 Aspose.Words では、HeadingFormat プロパティを使用して、すべてのページでヘッダー行を繰り返すことができます。
複数のヘッダー行がテーブルの先頭に連続して配置されている場合は、複数のヘッダー行をマークすることもできます。これを行うには、HeadingFormat プロパティをこれらの行に適用する必要があります。
次のコード例は、後続のページで繰り返されるヘッダー行を含むテーブルを構築する方法を示しています。
# 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.row_format.heading_format = True | |
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER | |
builder.cell_format.width = 100 | |
builder.insert_cell() | |
builder.writeln("Heading row 1") | |
builder.end_row() | |
builder.insert_cell() | |
builder.writeln("Heading row 2") | |
builder.end_row() | |
builder.cell_format.width = 50 | |
builder.paragraph_format.clear_formatting() | |
for _ in range(50): | |
builder.insert_cell() | |
builder.row_format.heading_format = False | |
builder.write("Column 1 Text") | |
builder.insert_cell() | |
builder.write("Column 2 Text") | |
builder.end_row() | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.repeat_rows_on_subsequent_pages.docx") |
テーブルと行がページをまたがらないようにする
表の内容を複数のページに分割すべきではない場合があります。たとえば、タイトルが表の上にある場合は、適切な外観を維持するために、タイトルと表を常に同じページ上にまとめて配置する必要があります。
この機能を実現するには、次の 2 つの個別の手法が役立ちます。
Allow row break across pages
、テーブル行に適用されますKeep with next
。表のセル内の段落に適用されます。
デフォルトでは、上記のプロパティは無効になっています。
行がページをまたがらないようにする
これには、行のセル内のコンテンツがページ全体に分割されないように制限することが含まれます。 Microsoft Word では、これはテーブルのプロパティの下に「ページをまたがる行の分割を許可する」オプションとして表示されます。 Aspose.Words では、これは Row の RowFormat オブジェクトの下にプロパティ RowFormat.AllowBreakAcrossPages として見つかります。
次のコード例は、テーブル内の各行のページ間での行の分割を無効にする方法を示しています。
# 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 + "Table spanning two pages.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# Disable breaking across pages for all rows in the table. | |
for row in table.rows: | |
row.as_row().row_format.allow_break_across_pages = False | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.row_format_disable_break_across_pages.docx") |
表がページをまたがらないようにする
テーブルがページ間で分割されないようにするには、テーブル内に含まれるコンテンツを一緒に保持することを指定する必要があります。
これを行うために、Aspose.Words はユーザーがテーブルを選択し、テーブル セル内の段落ごとに KeepWithNext パラメータを true に有効にするメソッドを使用します。例外はテーブルの最後の段落で、false に設定する必要があります。
次のコード例は、テーブルを同じページ上にまとめて配置するように設定する方法を示しています。
# 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 + "Table spanning two pages.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page, | |
# except for the last paragraphs in the last row of the table. | |
for cell in table.get_child_nodes(aw.NodeType.CELL, True): | |
cell = cell.as_cell() | |
cell.ensure_minimum() | |
for para in cell.paragraphs: | |
para = para.as_paragraph() | |
if not (cell.parent_row.is_last_row and para.is_end_of_cell): | |
para.paragraph_format.keep_with_next = True | |
doc.save(ARTIFACTS_DIR + "WorkingWithTables.keep_table_together.docx") |