Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Jun 17, 2025 11:42 am

HOW TO DUPLICATE MERGE CELL OR UNMERGE CELL in python

I extract by spire : | 8 | Xác minh thực địa | Không yêu cầu|

I want : |8 | Xác minh thực địa | Không yêu cầu | Không yêu cầu | Không yêu cầu|

this is my code:
for r in range(table.Rows.Count):
row_data = []
for c in range(table.Rows[r].Cells.Count):
cell_text = ""
for n in range(table.Rows[r].Cells[c].Paragraphs.Count):
cell_text +=' '+ table.Rows[r].Cells[c].Paragraphs[n].ListText.strip() + table.Rows[r].Cells[c].Paragraphs[n].Text.strip()
print('cell text:', cell_text)
row_data.append(cell_text)
table_data.append(row_data)

huyentn6
 
Posts: 3
Joined: Tue Jun 17, 2025 10:08 am

Wed Jun 18, 2025 9:46 am

Hello,

Thanks for your inquiry.
MERGE or UNMERGE cells can be copied in the following way, if there is any question, please feel free to write back.
Code: Select all
# Create a new Document instance
doc = Document()
doc.LoadFromFile("table.docx")

# Get the first section in the document
section = doc.Sections[0]

# Get the first table in the section
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

if table is not None:
    # Get the first row of the table
    firstRow = table.Rows[0]

    # Get the last cell in the first row
    lastCell = firstRow.Cells[firstRow.Cells.Count - 1]

    # Clone the last cell three times and add each clone to the end of the row
    for _ in range(3):
        cloned_cell = lastCell.Clone()
        firstRow.Cells.Add(cloned_cell)

# Save the modified document
doc.SaveToFile("result.docx", FileFormat.Docx2016)


Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Wed Jun 18, 2025 11:40 am

To be clear, for example: there is a cell that had been merged from 3 cells, I wanted to duplicate that cell 3 times.
The problem is:
a) I don't know how to determine which cell was merged (it can be last or first or center.vv.) in many rows
b) The number of cells that had been merged into one cell
c) I don't want to make change to the file or save to file, I only want it to read and extract the result as such.
Is there anyway to resolve these problems.
Thanks in regards!

huyentn6
 
Posts: 3
Joined: Tue Jun 17, 2025 10:08 am

Thu Jun 19, 2025 7:27 am

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
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Return to Spire.Doc

cron