Hello,
Thank you for your feedback.
The save example provided to you earlier was only for your convenience in viewing the effect of copying cells.
Table cells in Word have horizontal and vertical merged cells, and our Spire.Doc provides corresponding methods to determine merged cells. Please refer to the following code to implement your requirements. If there is any question, please feel free to write back.
- Code: Select all
def process_word_table(file_path, output_path):
# Create a Document object and load the Word file
doc = Document()
doc.LoadFromFile(file_path)
# Get the first Section and the first Table
section = doc.Sections[0]
table = section.Tables[0] if section.Tables.Count > 0 else None
if table is not None:
cells_to_clone = []
# Iterate through all rows and columns of the table
for i in range(table.Rows.Count):
row = table.Rows[i]
for j in range(row.Cells.Count):
cell = row.Cells[j]
# Get the text content of the cell
cell_text = ""
for k in range(cell.Paragraphs.Count):
paragraph = cell.Paragraphs[k]
cell_text += paragraph.Text + "\n"
print(f"Row {i}, Col {j} Content: {cell_text.strip()}")
gridSpan=cell.GridSpan
# Detect horizontal merging (GridSpan > 1)
if gridSpan > 1:
cells_to_clone.append(cell)
print(f"Row {i}, Col {j}: Horizontal merge, spanning {gridSpan} columns")
# Detect vertical merging
merge_type = cell.CellFormat.VerticalMerge
if merge_type != CellMerge.none:
if merge_type == CellMerge.Start:
print(f"Row {i}, Col {j}: Starting cell for merging cells")
elif merge_type == CellMerge.Continue:
print(f"Row {i}, Col {j}: Non-Starting cell for merging cells")
#Clone horizontally merged cells (example: clone each merged cell 3 times)
for cell in cells_to_clone:
row = table.Rows[0]
for _ in range(3):
cloned_cell = cell.Clone()
row.Cells.Add(cloned_cell)
Sincerely,
Tommy
E-iceblue support team