العمل مع الأعمدة والصفوف

لمزيد من التحكم في كيفية عمل الجداول، تعرف على كيفية التعامل مع الأعمدة والصفوف.

العثور على فهرس عناصر الجدول

تتم إدارة الأعمدة والصفوف والخلايا عن طريق الوصول إلى عقدة المستند المحددة من خلال فهرسها. يتضمن العثور على فهرس أي عقدة جمع كل العقد الفرعية لنوع العنصر من العقدة الأصلية، ثم استخدام طريقة 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 الخاص بـ 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، يمكن العثور على هذا ضمن خصائص الجدول كخيار “السماح للصف بالتقاطع عبر الصفحات”. في 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")