如何使用python-pptx库实现表格单元格的垂直或水平合并?
Hey there! I’ve tackled this exact issue before with python-pptx, so let me walk you through how to do both horizontal and vertical cell merges clearly. The library doesn’t have a single "merge cells" function out of the box, but you can achieve it using built-in properties for grid spanning and vertical merging.
Let’s start with a complete example that covers both merge types, then break down each part.
First, Set Up Your Basic Table
First, we’ll create a presentation, add a slide, and insert a table to work with:
from pptx import Presentation from pptx.util import Inches from pptx.enum.table import Merge # Create a new presentation and blank slide prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) # Blank slide layout # Add a 5-row, 4-column table left, top = Inches(1), Inches(1) width, height = Inches(6), Inches(4) table = slide.shapes.add_table(5, 4, left, top, width, height).table # Populate initial cell content (for reference) for row_idx in range(5): for col_idx in range(4): table.cell(row_idx, col_idx).text = f"Row {row_idx+1}, Col {col_idx+1}"
Horizontal Merge (Span Multiple Columns)
To merge cells horizontally, you’ll use the grid_span property of the starting cell. This tells PowerPoint how many columns the cell should occupy.
Example: Merge Row 1, Columns 1-3
# Target the starting cell (Row 1, Column 1 = index 0,0) start_cell = table.cell(0, 0) start_cell.text = "Horizontal Merge (3 Columns)" # Set grid_span to 3 to span 3 columns start_cell.grid_span = 3 # Clear content from the cells being merged into (optional but clean) for col_idx in range(1, 3): table.cell(0, col_idx).text = ""
Vertical Merge (Span Multiple Rows)
Vertical merging uses the v_merge enum to mark a starting cell and subsequent cells that should be merged into it.
Example: Merge Column 1, Rows 2-4
# Target the starting cell (Row 2, Column 1 = index 1,0) vertical_start = table.cell(1, 0) vertical_start.text = "Vertical Merge (3 Rows)" # Mark this as the first cell in the merged group vertical_start.v_merge = Merge.FIRST # Mark subsequent cells as part of the merge and clear their content for row_idx in range(2, 4): merged_cell = table.cell(row_idx, 0) merged_cell.v_merge = Merge.CONTINUE merged_cell.text = ""
Final Touches & Save
Adjust row heights/column widths if needed to make merged cells look clean, then save your presentation:
# Adjust row height for the horizontal merged row table.rows[0].height = Inches(0.8) # Adjust column width for the vertical merged column table.columns[0].width = Inches(1.5) # Save the presentation prs.save("merged_table_example.pptx")
Key Notes
- Only the starting cell’s content will be visible in the merged area, so always clear content from cells being merged into.
- For horizontal merges,
grid_spanmust match the number of columns you want to span (e.g., 2 for merging two columns). - For vertical merges, make sure every cell in the merged range has either
Merge.FIRST(only one) orMerge.CONTINUE(all others).
Hope this solves your problem! If you run into edge cases (like merging irregular ranges), feel free to follow up with details.
内容的提问来源于stack exchange,提问作者EugenS




