با ستون ها و ردیف ها کار کنید

برای کنترل بیشتر بر نحوه کار جداول، نحوه دستکاری ستون ها و ردیف ها را بیاموزید.

پیدا کردن شاخص عنصر جدول

ستون‌ها، ردیف‌ها و سلول‌ها با دسترسی به گره سند انتخاب شده توسط فهرست آن مدیریت می‌شوند. یافتن شاخص هر گره شامل جمع آوری تمام گره های فرزند از نوع عنصر از گره والد، و سپس استفاده از روش 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 تشکیل شده است. بنابراین، در Document Object Model Aspose.Words، مانند اسناد 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
view raw column-class.py hosted with ❤ by GitHub

مثال کد زیر نحوه درج یک ستون خالی در جدول را نشان می دهد:

# 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")

جداول و سطرها را از شکستن در صفحات جلوگیری کنید

مواقعی وجود دارد که محتویات یک جدول نباید در صفحات تقسیم شود. به عنوان مثال، اگر عنوانی در بالای جدول باشد، عنوان و جدول باید همیشه با هم در یک صفحه نگه داشته شوند تا ظاهر مناسب حفظ شود.

دو تکنیک جداگانه برای دستیابی به این عملکرد مفید هستند:

  • Allow row break across pages که برای ردیف های جدول اعمال می شود
  • Keep with next که روی پاراگراف های سلول های جدول اعمال می شود

به طور پیش فرض، ویژگی های فوق غیرفعال هستند.

یک ردیف را از شکستن در سراسر صفحات حفظ کنید

این شامل محدود کردن محتوای داخل سلول‌های یک ردیف از تقسیم شدن در یک صفحه است. در Microsoft Word، این مورد را می‌توان در زیر ویژگی‌های جدول به عنوان گزینه “Allow row to break through pages” یافت. در Aspose.Words این مورد در زیر شی RowFormat یک Row به عنوان ویژگی 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")