Tablo Stilini Uygula

Tablo stili, bir tabloya kolayca uygulanabilecek bir dizi biçimlendirmeyi tanımlar. Kenarlıklar, gölgeleme, hizalama ve yazı tipi gibi biçimlendirmeler tablo stilinde ayarlanabilir ve tutarlı bir görünüm için birçok tabloya uygulanabilir.

Aspose.Words, bir tabloya tablo stili uygulamayı ve ayrıca herhangi bir tablo stilinin özelliklerini okumayı destekler. Tablo stilleri yükleme ve kaydetme sırasında aşağıdaki şekillerde korunur:

  • DOCX ve WordML formatlarındaki tablo stilleri, bu formatlara yüklenirken ve kaydedilirken korunur
  • DOC formatında yüklerken ve kaydederken tablo stilleri korunur (ancak başka bir formatta değil)
  • Diğer formatlara dışa aktarırken, oluştururken veya yazdırırken, tablo stilleri tablodaki doğrudan formatlamaya genişletilir, böylece tüm formatlama korunur

Tablo Stili Oluşturun

Kullanıcı yeni bir stil oluşturabilir ve onu stil koleksiyonuna ekleyebilir. Add yöntemi yeni bir tablo stili oluşturmak için kullanılır.

Aşağıdaki kod örneği, yeni bir kullanıcı tanımlı tablo stilinin nasıl oluşturulacağını gösterir:

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

Mevcut Bir Tablo Stilini Kopyalama

Gerekirse belirli bir belgede zaten mevcut olan bir tablo stilini AddCopy yöntemini kullanarak stil koleksiyonunuza kopyalayabilirsiniz.

Bu kopyalamayla bağlantılı stillerin de kopyalandığını bilmek önemlidir.

Aşağıdaki kod örneği, bir stilin bir belgeden başka bir belgeye nasıl aktarılacağını gösterir:

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

Mevcut Bir Tablo Stilini Uygula

Aspose.Words, Style sınıfından miras alınan bir TableStyle sağlar. TableStyle, kullanıcının gölgeleme, dolgu, girinti, CellSpacing ve Font vb. gibi farklı stil seçeneklerini uygulamasını kolaylaştırır.

Ayrıca Aspose.Words, hangi tablo stiliyle çalışacağımızı belirtmek için StyleCollection sınıfını ve Table sınıfının birkaç özelliğini sağlar: Style, StyleIdentifier, StyleName ve StyleOptions.

Aspose.Words ayrıca, atanmış bir tablo stiliyle bir tablonun bazı alanlarına uygulanan özel formatlamayı temsil eden ConditionalStyle sınıfını ve ConditionalStyle nesnelerinin bir koleksiyonunu temsil eden ConditionalStyleCollection‘i de sağlar. Bu koleksiyon, ConditionalStyleType numaralandırma türünün her değeri için bir öğeyi temsil eden kalıcı bir öğe kümesi içerir. ConditionalStyleType numaralandırması, koşullu biçimlendirmenin bir tablo stilinde tanımlanabileceği tüm olası tablo alanlarını tanımlar.

Bu durumda, ConditionalStyleType numaralandırma türü altında tanımlanan tüm olası tablo alanları için koşullu biçimlendirme tanımlanabilir.

Aşağıdaki kod örneği, tablonun başlık satırı için koşullu biçimlendirmenin nasıl tanımlanacağını gösterir:

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

Ayrıca, ilk sütun, son sütun, şeritli satırlar gibi hangi tablo parçalarına stil uygulanacağını da seçebilirsiniz. TableStyleOptions numaralandırmasında listelenirler ve StyleOptions özelliği aracılığıyla uygulanırlar. TableStyleOptions numaralandırması bu özelliklerin bit düzeyinde birleşimine izin verir.

Aşağıdaki kod örneği, tablo stili uygulanmış yeni bir tablonun nasıl oluşturulacağını gösterir:

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

Aşağıdaki resimler Table Styles‘in Microsoft Word’deki temsilini ve Aspose.Words’deki karşılık gelen özelliklerini göstermektedir.

formatting-table-style-aspose-words-python

Tablo Stilleriyle Çalışmak

Tablo stili, bir tabloya kolayca uygulanabilecek bir dizi biçimlendirmeyi tanımlar. Kenarlıklar, gölgeleme, hizalama ve yazı tipi gibi biçimlendirmeler tablo stilinde ayarlanabilir ve tutarlı bir görünüm için birçok tabloya uygulanabilir.

Aspose.Words, bir tabloya tablo stili uygulamayı ve ayrıca herhangi bir tablo stilinin özelliklerini okumayı destekler. Tablo stilleri yükleme ve kaydetme sırasında aşağıdaki şekillerde korunur:

  • DOCX ve WordML formatlarındaki tablo stilleri, bu formatlara yüklenirken ve kaydedilirken korunur.
  • Tablo stilleri DOC formatında yüklenirken ve kaydedilirken korunur (ancak başka bir formatta değil).
  • Diğer formatlara dışa aktarırken, oluştururken veya yazdırırken, tablo stilleri tablodaki doğrudan formatlamaya genişletilir, böylece tüm formatlama korunur.

Şu anda yeni tablo stilleri oluşturamazsınız. Bir tabloya yalnızca yerleşik tablo stillerini veya belgede zaten mevcut olan özel tablo stillerini uygulayabilirsiniz

Tablo Stilinden Formatlamayı Alın ve Doğrudan Formatlama Olarak Uygulayın

Aspose.Words ayrıca bir tablo stilinde bulunan biçimlendirmeyi almak ve onu doğrudan biçimlendirme olarak tablonun satırlarına ve hücrelerine genişletmek için ExpandTableStylesToDirectFormatting yöntemini de sağlar. Biçimlendirmeyi tablo stili ve hücre stiliyle birleştirmeyi deneyin.

Aşağıdaki kod örneği, biçimlendirmenin stillerden tablo satırlarına ve hücrelere doğrudan biçimlendirme olarak nasıl genişletileceğini gösterir:

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