עיצוב שולחן

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

Aspose.Words תומך ביישום סגנון שולחן לשולחן וגם לקרוא תכונות של כל סגנון שולחן. סגנונות שולחן נשמרים במהלך טעינה והצלת בדרכים הבאות:

  • סגנונות שולחן בפורמט DOCX ו- WordML נשמרים בעת טעינה והצלת פורמטים אלה
  • סגנונות שולחן נשמרים בעת טעינה והצלת בפורמט DOC (אך לא לכל פורמט אחר)
    • כאשר ייצוא לפורמטים אחרים, ביצוע או הדפסה, סגנונות שולחן מורחבים לפורמט ישיר בטבלה, כך שכל הפורמט נשמר

יצירת סגנון שולחן

המשתמש יכול ליצור סגנון חדש ולהוסיף אותו לאוסף סגנון. The The The 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 מספק TableStyle תורשתי Style מעמד. TableStyle מאפשר למשתמש ליישם אפשרויות סגנון שונות כגון גילוח, padding, indentation, CellSpacing ו Font, וכו'.

בנוסף, Aspose.Words מספק StyleCollection שיעור וכמה תכונות של Table בכיתה כדי לציין איזה סגנון שולחן אנו עובדים עם: Style, StyleIdentifier, StyleName, ו StyleOptions.

Aspose.Words גם מספק ConditionalStyle מעמד המייצג פורמט מיוחד החל על שטח מסוים של שולחן עם סגנון שולחן מוקצה, ואת הפורמט המיוחד ConditionalStyleCollection זה מייצג אוסף ConditionalStyle אובייקטים. אוסף זה מכיל סט קבוע של פריטים המייצגים פריט אחד לכל ערך של ConditionalStyleType סוג של השראה The The The ConditionalStyleType enumeration מגדיר את כל אזורי השולחן האפשריים שאליה ניתן להגדיר פורמט מותני בסגנון שולחן.

במקרה זה, פורמט מותני יכול להיות מוגדר עבור כל אזור שולחן אפשרי מוגדר תחת סוג enumeration 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 רכוש. The The The TableStyleOptions enumeration מאפשר שילוב bitwise של תכונות אלה.

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

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

התמונות למטה מציגות ייצוג של Table Styles פנימה Microsoft Word התכונות המתאימות שלהם 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)