עבודה עם טורים ו Rows

לקבלת יותר שליטה על איך שולחנות לעבוד, ללמוד כיצד לתמרן עמודות ושורה.

למצוא את טבלת Element Index

טורים, שורות ותאים מנוהלים על ידי גישה למסמך שנבחר על ידי המדד שלו. מציאת האינדקס של כל צומת כרוך באיסוף כל אבני הילד של הסוג האלמנט מן הצומת ההורה, ולאחר מכן באמצעות שימוש 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 Node מורכב 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()

תגית: Header Rows

אתה יכול לבחור לחזור על השורה הראשונה בטבלה כמו Header Row רק בעמוד הראשון או על כל דף אם השולחן מחולק למספר. In In In Aspose.Words, אתה יכול לחזור על Header Row על כל דף באמצעות HeadingFormat רכוש.

אתה יכול גם לסמן שורות ראש מרובות אם שורות כאלה ממוקמות אחד לאחר השני בתחילת השולחן. כדי לעשות זאת, עליך ליישם את HeadingFormat תכונות שורות אלה.

הדוגמה הקודית הבאה מראה כיצד לבנות שולחן הכולל את Header Rows שחוזר על העמודים הבאים:

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

שמור שולחנות ו Rows מתוך Breaking Over Pages

ישנם זמנים שבהם התוכן של שולחן לא צריך להיות מחולק על פני דפים. לדוגמה, אם כותרת היא מעל שולחן, הכותרת והשולחן צריכים תמיד להיות יחד באותו דף כדי לשמור על המראה הנכון.

ישנן שתי טכניקות נפרדות שימושיות להשגת פונקציונליות זו:

    • Allow row break across pages, המונחים: Tables
    • Keep with next, אשר חל על פסקאות בתאי שולחן

כברירת מחדל, התכונות לעיל מוגבלות.

עקבו אחרי Breaking Overs Pages

זה כרוך הגבלת תוכן בתוך התאים של שורה מלהיות מפוצל על פני דף. In In In Microsoft Word, זה יכול למצוא תחת טבלה Properties כאפשרות “לעמוד שורות כדי לפרוץ דפים”. In In In 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")

עקבו אחרי Breaking Overs Pages

כדי לעצור את השולחן מפיצול בדפים, עלינו לציין שאנו רוצים שהתוכן הכלול בתוך השולחן כדי להישאר יחד.

לעשות את זה, 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")