표 스타일 적용

표 스타일은 표에 쉽게 적용할 수 있는 서식 집합을 정의합니다. 테두리, 음영, 정렬, 글꼴 등의 서식을 표 스타일로 설정하고 일관된 모양을 위해 여러 표에 적용할 수 있습니다.

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는 Style 클래스에서 상속된 TableStyle를 제공합니다. TableStyle를 사용하면 사용자가 음영, 패딩, 들여쓰기, CellSpacingFont 등과 같은 다양한 스타일 옵션을 적용할 수 있습니다.

또한 Aspose.Words는 작업할 테이블 스타일(Style, StyleIdentifier, StyleNameStyleOptions)을 지정하기 위해 StyleCollection 클래스와 Table 클래스의 몇 가지 속성을 제공합니다.

Aspose.Words는 또한 할당된 테이블 스타일을 사용하여 테이블의 일부 영역에 적용된 특수 서식을 나타내는 ConditionalStyle 클래스와 ConditionalStyle 개체 컬렉션을 나타내는 ConditionalStyleCollection을 제공합니다. 이 컬렉션에는 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")

아래 그림은 Microsoft Word의 Table Styles 표현과 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)