|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import copy |
| 6 | +import re |
5 | 7 | from typing import IO, TYPE_CHECKING, cast |
6 | 8 |
|
7 | 9 | from pptx.enum.shapes import PROG_ID |
8 | 10 | from pptx.opc.constants import CONTENT_TYPE as CT |
9 | 11 | from pptx.opc.constants import RELATIONSHIP_TYPE as RT |
10 | | -from pptx.opc.package import XmlPart |
| 12 | +from pptx.opc.package import Part, XmlPart |
11 | 13 | from pptx.opc.packuri import PackURI |
12 | 14 | from pptx.oxml.slide import CT_NotesMaster, CT_NotesSlide, CT_Slide |
13 | 15 | from pptx.oxml.theme import CT_OfficeStyleSheet |
@@ -259,6 +261,173 @@ def _add_notes_slide_part(self): |
259 | 261 | self.relate_to(notes_slide_part, RT.NOTES_SLIDE) |
260 | 262 | return notes_slide_part |
261 | 263 |
|
| 264 | + def duplicate(self) -> SlidePart: |
| 265 | + """Return a new |SlidePart| that is a deep copy of this one. |
| 266 | +
|
| 267 | + Image, media, slide-layout, and slide-master rels are reused — |
| 268 | + the duplicate references the same package-level parts as the |
| 269 | + source. Chart, OLE-embedded, and embedded-package parts are |
| 270 | + deep-copied per duplicate. The notes-slide rel and any |
| 271 | + comments rels are NOT carried over: notes-slide rewiring is |
| 272 | + the caller's job (see |Slides.duplicate|), and comments are |
| 273 | + out of scope for Phase 2 of issue #11. |
| 274 | + """ |
| 275 | + new_partname = self._package.next_partname("/ppt/slides/slide%d.xml") |
| 276 | + new_element = copy.deepcopy(self._element) |
| 277 | + new_part = SlidePart(new_partname, CT.PML_SLIDE, self._package, new_element) |
| 278 | + |
| 279 | + rId_map = _replicate_rels_for_duplicate(self, new_part) |
| 280 | + _remap_rId_attrs(new_element, rId_map) |
| 281 | + |
| 282 | + return new_part |
| 283 | + |
| 284 | + |
| 285 | +# --------------------------------------------------------------------------- |
| 286 | +# Module-level helpers for slide / slide-private part duplication. |
| 287 | +# --------------------------------------------------------------------------- |
| 288 | + |
| 289 | +_RELS_NS = "{http://schemas.openxmlformats.org/officeDocument/2006/relationships}" |
| 290 | + |
| 291 | +# Reltypes filtered out during slide duplication. NOTES_SLIDE is wired |
| 292 | +# explicitly by |Slides.duplicate| so the new notes-slide back-references |
| 293 | +# the new parent slide. Comments are dropped — Phase 2 scope (issue #11). |
| 294 | +_DUP_DROP_RELTYPES_SLIDE = frozenset({RT.NOTES_SLIDE, RT.COMMENTS, RT.COMMENT_AUTHORS}) |
| 295 | + |
| 296 | + |
| 297 | +def _replicate_rels_for_duplicate(src_part: Part, new_part: Part) -> dict[str, str]: |
| 298 | + """Mirror src_part's slide-relevant rels onto new_part. |
| 299 | +
|
| 300 | + Returns a `{old_rId: new_rId}` map for rId-attribute remapping. |
| 301 | + """ |
| 302 | + rId_map: dict[str, str] = {} |
| 303 | + for rId, rel in src_part.rels.items(): |
| 304 | + if rel.reltype in _DUP_DROP_RELTYPES_SLIDE: |
| 305 | + continue |
| 306 | + if rel.is_external: |
| 307 | + new_rId = new_part.relate_to(rel.target_ref, rel.reltype, is_external=True) |
| 308 | + elif rel.reltype == RT.CHART: |
| 309 | + new_target = _duplicate_chart_part(cast(ChartPart, rel.target_part)) |
| 310 | + new_rId = new_part.relate_to(new_target, rel.reltype) |
| 311 | + elif rel.reltype in (RT.OLE_OBJECT, RT.PACKAGE): |
| 312 | + new_target = _duplicate_blob_part(cast(Part, rel.target_part)) |
| 313 | + new_rId = new_part.relate_to(new_target, rel.reltype) |
| 314 | + else: |
| 315 | + # Shared parts: image, media, video, layout, master, theme, etc. |
| 316 | + new_rId = new_part.relate_to(rel.target_part, rel.reltype) |
| 317 | + rId_map[rId] = new_rId |
| 318 | + return rId_map |
| 319 | + |
| 320 | + |
| 321 | +def _remap_rId_attrs(element, rId_map: dict[str, str]) -> None: |
| 322 | + """Substitute relationships-namespace attribute values in `element`. |
| 323 | +
|
| 324 | + Walks every descendant element and rewrites any attribute whose name |
| 325 | + is in the OOXML relationships namespace (catches `r:id`, `r:embed`, |
| 326 | + `r:link`, `r:pict`, `r:href` in one pass). |
| 327 | + """ |
| 328 | + for el in element.iter(): |
| 329 | + for attr_name in list(el.attrib): |
| 330 | + if attr_name.startswith(_RELS_NS): |
| 331 | + old = el.attrib[attr_name] |
| 332 | + if old in rId_map: |
| 333 | + el.attrib[attr_name] = rId_map[old] |
| 334 | + |
| 335 | + |
| 336 | +def _duplicate_chart_part(src: ChartPart) -> ChartPart: |
| 337 | + """Return a new ChartPart cloning `src`. |
| 338 | +
|
| 339 | + Chart XML is deep-copied. Embedded data (e.g. an xlsx workbook |
| 340 | + reached via an `RT.PACKAGE` rel) is binary and must be blob-copied, |
| 341 | + not deep-copy-of-XML — the workbook IS the chart's data, and the |
| 342 | + `<c:numCache>` values in the chart XML mirror it. |
| 343 | + """ |
| 344 | + package = src._package |
| 345 | + new_partname = package.next_partname("/ppt/charts/chart%d.xml") |
| 346 | + new_element = copy.deepcopy(src._element) |
| 347 | + cls = type(src) |
| 348 | + new_part = cls(new_partname, src.content_type, package, new_element) |
| 349 | + rId_map: dict[str, str] = {} |
| 350 | + for rId, rel in src.rels.items(): |
| 351 | + if rel.is_external: |
| 352 | + new_rId = new_part.relate_to(rel.target_ref, rel.reltype, is_external=True) |
| 353 | + elif rel.reltype == RT.PACKAGE: |
| 354 | + new_target = _duplicate_blob_part(cast(Part, rel.target_part)) |
| 355 | + new_rId = new_part.relate_to(new_target, rel.reltype) |
| 356 | + else: |
| 357 | + # Theme override and other chart-private parts: share for now. |
| 358 | + # Practical impact is small; revisit if a user reports it. |
| 359 | + new_rId = new_part.relate_to(rel.target_part, rel.reltype) |
| 360 | + rId_map[rId] = new_rId |
| 361 | + _remap_rId_attrs(new_element, rId_map) |
| 362 | + return new_part |
| 363 | + |
| 364 | + |
| 365 | +def _duplicate_blob_part(src: Part) -> Part: |
| 366 | + """Return a new binary |Part| cloning `src`'s blob. |
| 367 | +
|
| 368 | + Used for embedded packages (xlsx, docx, pptx) and OLE objects — |
| 369 | + parts whose payload is opaque bytes rather than XML. |
| 370 | + """ |
| 371 | + package = src._package |
| 372 | + cls = type(src) |
| 373 | + tmpl = getattr(cls, "partname_template", None) |
| 374 | + if tmpl is None: |
| 375 | + tmpl = _derive_partname_template(str(src.partname)) |
| 376 | + new_partname = package.next_partname(tmpl) |
| 377 | + return cls(new_partname, src.content_type, package, src.blob) |
| 378 | + |
| 379 | + |
| 380 | +def _derive_partname_template(partname: str) -> str: |
| 381 | + """Derive a `next_partname`-compatible template from an existing partname. |
| 382 | +
|
| 383 | + Replaces the trailing integer (just before the final extension) with |
| 384 | + `%d`. Falls back to inserting `%d` immediately before the extension |
| 385 | + if there is no trailing digit run. |
| 386 | + """ |
| 387 | + match = re.match(r"^(.*?)(\d+)(\.[^./]+)$", partname) |
| 388 | + if match: |
| 389 | + prefix, _, ext = match.groups() |
| 390 | + return f"{prefix}%d{ext}" |
| 391 | + # No trailing-digit pattern; insert %d before final extension. |
| 392 | + dot = partname.rfind(".") |
| 393 | + if dot < 0: |
| 394 | + return f"{partname}%d" |
| 395 | + return f"{partname[:dot]}%d{partname[dot:]}" |
| 396 | + |
| 397 | + |
| 398 | +def duplicate_notes_slide_for( |
| 399 | + src_slide_part: SlidePart, new_slide_part: SlidePart |
| 400 | +) -> NotesSlidePart: |
| 401 | + """Create a fresh |NotesSlidePart| for `new_slide_part`, cloning content from src. |
| 402 | +
|
| 403 | + Public-to-the-module helper used by |Slides.duplicate| AFTER the new |
| 404 | + slide part is registered with the presentation rels. Wires the new |
| 405 | + notes-slide's `RT.SLIDE` back-rel to point at `new_slide_part` (NOT |
| 406 | + the source) — addresses upstream community gotcha #961 where blindly |
| 407 | + copying notes rels left the duplicate's notes pointing at the source. |
| 408 | + """ |
| 409 | + src_notes_part = cast(NotesSlidePart, src_slide_part.part_related_by(RT.NOTES_SLIDE)) |
| 410 | + package = src_slide_part._package |
| 411 | + new_partname = package.next_partname("/ppt/notesSlides/notesSlide%d.xml") |
| 412 | + new_element = copy.deepcopy(src_notes_part._element) |
| 413 | + new_notes_part = NotesSlidePart(new_partname, CT.PML_NOTES_SLIDE, package, new_element) |
| 414 | + |
| 415 | + rId_map: dict[str, str] = {} |
| 416 | + for rId, rel in src_notes_part.rels.items(): |
| 417 | + if rel.is_external: |
| 418 | + new_rId = new_notes_part.relate_to(rel.target_ref, rel.reltype, is_external=True) |
| 419 | + elif rel.reltype == RT.SLIDE: |
| 420 | + # ---rewire back-ref to NEW slide part--- |
| 421 | + new_rId = new_notes_part.relate_to(new_slide_part, RT.SLIDE) |
| 422 | + else: |
| 423 | + # NOTES_MASTER and any others: share at package level |
| 424 | + new_rId = new_notes_part.relate_to(rel.target_part, rel.reltype) |
| 425 | + rId_map[rId] = new_rId |
| 426 | + _remap_rId_attrs(new_element, rId_map) |
| 427 | + |
| 428 | + new_slide_part.relate_to(new_notes_part, RT.NOTES_SLIDE) |
| 429 | + return new_notes_part |
| 430 | + |
262 | 431 |
|
263 | 432 | class SlideLayoutPart(BaseSlidePart): |
264 | 433 | """Slide layout part. |
|
0 commit comments