Commit bcf58ca
feat(tables): add rows.add/remove + columns.add/remove (Tables 2.0 Phase 1)
Phase 1 of issue #12 (Tables 2.0 epic). Lands the second-most-asked
table feature in the python-pptx ecosystem — adding and removing rows
and columns on existing tables (upstream issue scanny#86,
17 comments; closes the upstream PR scanny#399 design directly). Defers
table-style binding (`apply_style`), merge ergonomics, and gridSpan
accessor exposure to follow-up phases.
New public API
--------------
- `Table.rows.add(at=None, height=None) -> _Row`
* `at=None` (default) appends at the bottom; backwards compatible.
* `at=N` inserts at zero-based position N; `at=len(rows)` appends.
* `height=None` inherits the first row's height (or 0.4" for an
empty table). Pass an explicit `Length` to override.
* Populates the new row with empty `<a:tc>` cells matching the
current column count.
- `Table.rows.remove(index)`
* Drops the row at `index`. Raises `ValueError` when the row has
a cell with `rowSpan > 1` (origin) or `vMerge=True` (target) —
removing such a row would orphan the rest of a vertical merge.
Split the affected merge first.
- `Table.columns.add(at=None, width=None) -> _Column`
* Mirrors `rows.add` semantics. Inserts an empty `<a:tc>` at the
corresponding position in every existing row, preserving cell
alignment. `width=None` inherits the leftmost column's width
(or 1" for an empty table).
- `Table.columns.remove(index)`
* Drops the gridCol at `index` and the corresponding `<a:tc>` from
every row. Raises `ValueError` on a column carrying `gridSpan > 1`
or `hMerge=True`.
- `_RowCollection.__iter__` and `_ColumnCollection.__iter__` are
promoted to first-class iterators (existed implicitly before via
`__getitem__`; explicit makes intent clear and matches Pythonic
collection conventions).
Internal additions
------------------
- `pptx/oxml/table.py`:
* `CT_TableGrid.{insert_gridCol_at, remove_gridCol_at}`
* `CT_Table.{insert_tr_at, remove_tr_at, column_has_cross_column_merge}`
* `CT_TableRow.{insert_tc_at, remove_tc_at, has_cross_row_merge}`
Merge-safety policy (Phase 1)
-----------------------------
This phase deliberately raises `ValueError` rather than silently
mutating the merge graph when row/column CRUD would split a merge:
| Operation | Raises when |
|--------------------------------------|--------------------------------------|
| `rows.remove(idx)` | row contains `rowSpan>1` or `vMerge` |
| `rows.add(at=idx)` | row at `idx` has any `vMerge=True` |
| `columns.remove(idx)` | col has `gridSpan>1` or `hMerge` |
| `columns.add(at=idx)` | col `idx` has any `hMerge=True` |
Auto-splitting merges through CRUD is a Phase 2 enhancement.
Test coverage
-------------
- 55 new unit tests in `tests/test_tables_crud.py`:
* 18 oxml-layer tests (insert/remove/cross-merge detection)
* 32 collection-API tests (rows.add/remove + columns.add/remove)
* 5 round-trip integration tests (open → mutate → save → reopen)
- 7 new behave scenarios in `features/tbl-crud.feature` covering
append-row, indexed-row-insert, append-column, remove-row,
remove-column, and the two merge-safety raise cases.
- New `uat_tables_crud.py` (untracked per repo §6) builds a 5-slide
deck where each slide shows a different stage of CRUD operations,
printing per-stage cell content so the maintainer can flip through
in PowerPoint and see the mutations land.
Verification
------------
```
$ python3 -m pytest tests/ -q | tail -3
3277 passed in 4.54s
$ ruff check src tests | tail -3
All checks passed!
$ python3 -m behave features/ --no-color | tail -3
1006 scenarios passed, 0 failed, 0 skipped
3023 steps passed, 0 failed, 0 skipped
```
Refs #121 parent 76291a2 commit bcf58ca
5 files changed
Lines changed: 845 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
48 | 71 | | |
49 | 72 | | |
50 | 73 | | |
| |||
136 | 159 | | |
137 | 160 | | |
138 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
139 | 177 | | |
140 | 178 | | |
141 | 179 | | |
| |||
439 | 477 | | |
440 | 478 | | |
441 | 479 | | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
442 | 502 | | |
443 | 503 | | |
444 | 504 | | |
| |||
464 | 524 | | |
465 | 525 | | |
466 | 526 | | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
467 | 561 | | |
468 | 562 | | |
469 | 563 | | |
| |||
0 commit comments