ใช้สไตล์ตาราง

สไตล์ตารางจะกำหนดชุดการจัดรูปแบบที่สามารถนำไปใช้กับตารางได้อย่างง่ายดาย การจัดรูปแบบ เช่น เส้นขอบ การแรเงา การจัดแนว และแบบอักษรสามารถตั้งค่าในลักษณะตารางและนำไปใช้กับตารางจำนวนมากเพื่อให้มีลักษณะที่สอดคล้องกัน

Aspose.Words รองรับการใช้สไตล์ตารางกับตารางและยังอ่านคุณสมบัติของสไตล์ตารางใดก็ได้ สไตล์ตารางจะถูกรักษาไว้ในระหว่างการโหลดและบันทึกด้วยวิธีต่อไปนี้:

  • สไตล์ตารางในรูปแบบ DOCX และ WordML จะถูกรักษาไว้เมื่อโหลดและบันทึกเป็นรูปแบบเหล่านี้
  • สไตล์ตารางจะยังคงอยู่เมื่อโหลดและบันทึกในรูปแบบ DOC (แต่ไม่ใช่รูปแบบอื่น)
  • เมื่อส่งออกเป็นรูปแบบอื่น การเรนเดอร์หรือการพิมพ์ รูปแบบตารางจะถูกขยายไปสู่การจัดรูปแบบโดยตรงในตาราง ดังนั้นการจัดรูปแบบทั้งหมดจึงยังคงอยู่

สร้างสไตล์ตาราง

ผู้ใช้สามารถสร้างสไตล์ใหม่และเพิ่มลงในคอลเลกชันสไตล์ได้ วิธีการ 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 อำนวยความสะดวกให้ผู้ใช้ใช้ตัวเลือกสไตล์ต่างๆ เช่น การแรเงา การเติม การเยื้อง CellSpacing และ Font เป็นต้น

นอกจากนี้ Aspose.Words ยังมีคลาส StyleCollection และคุณสมบัติบางอย่างของคลาส Table เพื่อระบุรูปแบบตารางที่เราจะใช้งาน: Style, StyleIdentifier, StyleName และ StyleOptions

Aspose.Words ยังมีคลาส ConditionalStyle ที่แสดงการจัดรูปแบบพิเศษที่ใช้กับพื้นที่บางส่วนของตารางด้วยสไตล์ตารางที่กำหนด และ ConditionalStyleCollection ที่แสดงคอลเลกชันของออบเจ็กต์ ConditionalStyle คอลเลกชันนี้ประกอบด้วยชุดรายการถาวรซึ่งแสดงถึงหนึ่งรายการสำหรับแต่ละค่าของประเภทการแจงนับ ConditionalStyleType การแจงนับ ConditionalStyleType จะกำหนดพื้นที่ตารางที่เป็นไปได้ทั้งหมดซึ่งอาจกำหนดการจัดรูปแบบตามเงื่อนไขในลักษณะตารางได้

ในกรณีนี้ การจัดรูปแบบตามเงื่อนไขสามารถกำหนดได้สำหรับพื้นที่ตารางที่เป็นไปได้ทั้งหมดที่กำหนดไว้ภายใต้ประเภทการแจงนับ 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 การแจงนับ TableStyleOptions อนุญาตให้รวมคุณลักษณะเหล่านี้ในระดับบิต

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างตารางใหม่โดยใช้สไตล์ตาราง:

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