| 1 | """Slide XML and slide relationship XML generation.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pptx_transitions import create_transition_xml |
| 6 | |
| 7 | |
| 8 | def create_slide_xml_with_svg( |
| 9 | slide_num: int, |
| 10 | png_rid: str, |
| 11 | svg_rid: str, |
| 12 | width_emu: int, |
| 13 | height_emu: int, |
| 14 | transition: str | None = 'fade', |
| 15 | transition_duration: float = 0.5, |
| 16 | auto_advance: float | None = None, |
| 17 | use_compat_mode: bool = True, |
| 18 | transition_effect_options: dict[str, object] | None = None, |
| 19 | ) -> str: |
| 20 | """Create slide XML containing an SVG image. |
| 21 | |
| 22 | Args: |
| 23 | slide_num: Slide number. |
| 24 | png_rid: PNG fallback image relationship ID. |
| 25 | svg_rid: SVG relationship ID. |
| 26 | width_emu: Width in EMU. |
| 27 | height_emu: Height in EMU. |
| 28 | transition: Transition effect name. |
| 29 | transition_effect_options: PowerPoint Effect Options for the selected |
| 30 | native transition. |
| 31 | transition_duration: Transition duration in seconds. |
| 32 | auto_advance: Auto-advance interval in seconds. |
| 33 | use_compat_mode: Whether to use compatibility mode (PNG + SVG dual format). |
| 34 | """ |
| 35 | transition_xml = '' |
| 36 | if transition is not None or auto_advance is not None: |
| 37 | transition_fragment = create_transition_xml( |
| 38 | effect=transition, |
| 39 | duration=transition_duration, |
| 40 | advance_after=auto_advance, |
| 41 | effect_options=transition_effect_options, |
| 42 | ) |
| 43 | if transition_fragment: |
| 44 | transition_xml = '\n' + transition_fragment |
| 45 | |
| 46 | if use_compat_mode: |
| 47 | blip_xml = f'''<a:blip r:embed="{png_rid}"> |
| 48 | <a:extLst> |
| 49 | <a:ext uri="{{96DAC541-7B7A-43D3-8B79-37D633B846F1}}"> |
| 50 | <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="{svg_rid}"/> |
| 51 | </a:ext> |
| 52 | </a:extLst> |
| 53 | </a:blip>''' |
| 54 | else: |
| 55 | blip_xml = f'<a:blip r:embed="{svg_rid}"/>' |
| 56 | |
| 57 | return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 58 | <p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" |
| 59 | xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" |
| 60 | xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"> |
| 61 | <p:cSld> |
| 62 | <p:spTree> |
| 63 | <p:nvGrpSpPr> |
| 64 | <p:cNvPr id="1" name=""/> |
| 65 | <p:cNvGrpSpPr/> |
| 66 | <p:nvPr/> |
| 67 | </p:nvGrpSpPr> |
| 68 | <p:grpSpPr> |
| 69 | <a:xfrm> |
| 70 | <a:off x="0" y="0"/> |
| 71 | <a:ext cx="0" cy="0"/> |
| 72 | <a:chOff x="0" y="0"/> |
| 73 | <a:chExt cx="0" cy="0"/> |
| 74 | </a:xfrm> |
| 75 | </p:grpSpPr> |
| 76 | <p:pic> |
| 77 | <p:nvPicPr> |
| 78 | <p:cNvPr id="2" name="SVG Image {slide_num}"/> |
| 79 | <p:cNvPicPr> |
| 80 | <a:picLocks noChangeAspect="1"/> |
| 81 | </p:cNvPicPr> |
| 82 | <p:nvPr/> |
| 83 | </p:nvPicPr> |
| 84 | <p:blipFill> |
| 85 | {blip_xml} |
| 86 | <a:stretch> |
| 87 | <a:fillRect/> |
| 88 | </a:stretch> |
| 89 | </p:blipFill> |
| 90 | <p:spPr> |
| 91 | <a:xfrm> |
| 92 | <a:off x="0" y="0"/> |
| 93 | <a:ext cx="{width_emu}" cy="{height_emu}"/> |
| 94 | </a:xfrm> |
| 95 | <a:prstGeom prst="rect"> |
| 96 | <a:avLst/> |
| 97 | </a:prstGeom> |
| 98 | </p:spPr> |
| 99 | </p:pic> |
| 100 | </p:spTree> |
| 101 | </p:cSld> |
| 102 | <p:clrMapOvr> |
| 103 | <a:masterClrMapping/> |
| 104 | </p:clrMapOvr>{transition_xml} |
| 105 | </p:sld>''' |
| 106 | |
| 107 | |
| 108 | def create_slide_rels_xml( |
| 109 | png_rid: str, |
| 110 | png_filename: str, |
| 111 | svg_rid: str, |
| 112 | svg_filename: str, |
| 113 | use_compat_mode: bool = True, |
| 114 | slide_layout_target: str = "../slideLayouts/slideLayout1.xml", |
| 115 | ) -> str: |
| 116 | """Create slide relationship file XML. |
| 117 | |
| 118 | Args: |
| 119 | png_rid: PNG image relationship ID. |
| 120 | png_filename: PNG filename. |
| 121 | svg_rid: SVG relationship ID. |
| 122 | svg_filename: SVG filename. |
| 123 | use_compat_mode: Whether to use compatibility mode. |
| 124 | slide_layout_target: Target for the slide's actual layout part. |
| 125 | """ |
| 126 | if use_compat_mode: |
| 127 | return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 128 | <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 129 | <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="{slide_layout_target}"/> |
| 130 | <Relationship Id="{png_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{png_filename}"/> |
| 131 | <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/> |
| 132 | </Relationships>''' |
| 133 | else: |
| 134 | return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 135 | <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 136 | <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="{slide_layout_target}"/> |
| 137 | <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/> |
| 138 | </Relationships>''' |
| 139 |