표 셀 병합

때로는 표의 특정 행에 표의 전체 너비를 차지하는 제목이나 큰 텍스트 블록이 필요한 경우가 있습니다. 테이블의 적절한 디자인을 위해 사용자는 여러 테이블 셀을 하나로 병합할 수 있습니다. Aspose.Words는 HTML 콘텐츠 가져오기를 포함하여 모든 입력 형식으로 작업할 때 병합된 셀을 지원합니다.

표 셀을 병합하는 방법

Aspose.Words에서 병합된 셀은 CellFormat 클래스의 다음 속성으로 표시됩니다

  • 셀이 수평 셀 병합의 일부인지 설명하는 HorizontalMerge
  • 셀이 셀의 수직 병합의 일부인지 설명하는 VerticalMerge

이러한 속성 값은 셀의 병합 동작을 결정합니다

셀이 병합되었는지 확인

셀이 병합된 셀 시퀀스의 일부인지 확인하려면 HorizontalMergeVerticalMerge 속성을 확인하면 됩니다.

다음 코드 예제에서는 가로 및 세로 셀 병합 유형을 인쇄하는 방법을 보여줍니다

# 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 with merged cells.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
for row in table.rows:
for cell in row.as_row().cells:
print(self.print_cell_merge_type(cell.as_cell()))

DocumentBuilder 사용 시 테이블 셀 병합

DocumentBuilder로 생성된 테이블의 셀을 병합하려면 병합이 예상되는 각 셀에 대해 적절한 병합 유형(먼저 CellMerge.First, 그 다음 CellMerge.Previous)을 설정해야 합니다.

또한 병합이 필요하지 않은 셀에 대한 병합 설정을 지워야 한다는 점을 기억해야 합니다. 이는 병합되지 않은 첫 번째 셀을 CellMerge.None로 설정하여 수행할 수 있습니다. 그렇지 않으면 테이블의 모든 셀이 병합됩니다.

다음 코드 예제에서는 첫 번째 행의 셀이 가로로 병합되는 두 개의 행이 있는 테이블을 만드는 방법을 보여줍니다

# 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.insert_cell()
builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST
builder.write("Text in merged cells.")
builder.insert_cell()
# This cell is merged to the previous and should be empty.
builder.cell_format.horizontal_merge = aw.tables.CellMerge.PREVIOUS
builder.end_row()
builder.insert_cell()
builder.cell_format.horizontal_merge = aw.tables.CellMerge.NONE
builder.write("Text in one cell.")
builder.insert_cell()
builder.write("Text in another cell.")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTables.horizontal_merge.docx")

다음 코드 예제에서는 첫 번째 열의 셀이 수직으로 병합되는 2열 테이블을 만드는 방법을 보여줍니다

# 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.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST
builder.write("Text in merged cells.")
builder.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE
builder.write("Text in one cell")
builder.end_row()
builder.insert_cell()
# This cell is vertically merged to the cell above and should be empty.
builder.cell_format.vertical_merge = aw.tables.CellMerge.PREVIOUS
builder.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE
builder.write("Text in another cell")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTables.vertical_merge.docx")

다른 경우의 표 셀 병합

기존 테이블과 같이 DocumentBuilder를 사용하지 않는 다른 상황에서는 이전 방식으로 셀을 병합하는 것이 쉽지 않을 수 있습니다. 대신 작업을 훨씬 쉽게 만드는 방법으로 셀에 병합 속성을 적용하는 것과 관련된 기본 작업을 래핑할 수 있습니다. 이 방법은 테이블의 셀 범위를 병합하기 위해 호출되는 병합 자동화 방법과 유사합니다.

아래 코드는 지정된 셀에서 시작하여 끝 셀에서 끝나는 지정된 범위의 테이블 셀을 병합합니다. 이 경우 범위는 여러 행이나 열에 걸쳐 있을 수 있습니다

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
@staticmethod
def merge_cells(start_cell: aw.tables.Cell, end_cell: aw.tables.Cell):
parent_table = start_cell.parent_row.parent_table
# Find the row and cell indices for the start and end cell.
start_cell_pos = drawing.Point(start_cell.parent_row.index_of(start_cell), parent_table.index_of(start_cell.parent_row))
end_cell_pos = drawing.Point(end_cell.parent_row.index_of(end_cell), parent_table.index_of(end_cell.parent_row))
# Create a range of cells to be merged based on these indices.
# Inverse each index if the end cell is before the start cell.
merge_range = drawing.Rectangle(
min(start_cell_pos.x, end_cell_pos.x),
min(start_cell_pos.y, end_cell_pos.y),
abs(end_cell_pos.x - start_cell_pos.x) + 1,
abs(end_cell_pos.y - start_cell_pos.y) + 1)
for row in parent_table.rows:
row = row.as_row()
for cell in row.cells:
cell = cell.as_cell()
current_pos = drawing.Point(row.index_of(cell), parent_table.index_of(row))
# Check if the current cell is inside our merge range, then merge it.
if merge_range.contains(current_pos):
cell.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST if current_pos.x == merge_range.x else aw.tables.CellMerge.PREVIOUS
cell.cell_format.vertical_merge = aw.tables.CellMerge.FIRST if current_pos.y == merge_range.y else aw.tables.CellMerge.PREVIOUS
view raw merge-cells.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 + "Table with merged cells.docx")
table = doc.first_section.body.tables[0]
# We want to merge the range of cells found inbetween these two cells.
cell_start_range = table.rows[0].cells[0]
cell_end_range = table.rows[1].cells[1]
# Merge all the cells between the two specified cells into one.
self.merge_cells(cell_start_range, cell_end_range)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.merge_cell_range.docx")

사용 중인 프레임워크 버전에 따라 이 메서드를 확장 메서드로 전환하여 개선할 수 있습니다. 이 경우 셀에서 직접 이 메서드를 호출하여 cell1.Merge(cell2)와 같은 셀 범위를 병합할 수 있습니다.

가로로 병합된 셀로 변환

일부 최신 버전의 Microsoft Word는 셀이 수평으로 병합될 때 더 이상 병합 플래그를 사용하지 않기 때문에 어떤 셀이 병합되었는지 감지할 수 없는 경우가 있습니다. 그러나 병합 플래그를 사용하여 셀이 너비만큼 수평으로 셀에 병합되는 상황의 경우 Aspose.Words는 셀을 변환하는 ConvertToHorizontallyMergedCells 방법을 제공합니다. 이 방법은 단순히 테이블을 변환하고 필요에 따라 새 셀을 추가합니다.

다음 코드 예제에서는 위의 메서드가 작동하는 모습을 보여줍니다

# 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 with merged cells.docx")
table = doc.first_section.body.tables[0]
# Now merged cells have appropriate merge flags.
table.convert_to_horizontally_merged_cells()