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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
"""lxml custom element class for CT_GraphicalObjectFrame XML element."""
from __future__ import annotations
from typing import TYPE_CHECKING, cast
from pptx.oxml import parse_xml
from pptx.oxml.chart.chart import CT_Chart
from pptx.oxml.ns import nsdecls
from pptx.oxml.shapes.shared import BaseShapeElement
from pptx.oxml.simpletypes import XsdBoolean, XsdString
from pptx.oxml.table import CT_Table
from pptx.oxml.xmlchemy import (
BaseOxmlElement,
OneAndOnlyOne,
OptionalAttribute,
RequiredAttribute,
ZeroOrOne,
)
from pptx.spec import (
GRAPHIC_DATA_URI_CHART,
GRAPHIC_DATA_URI_OLEOBJ,
GRAPHIC_DATA_URI_TABLE,
)
if TYPE_CHECKING:
from pptx.oxml.shapes.shared import (
CT_ApplicationNonVisualDrawingProps,
CT_NonVisualDrawingProps,
CT_Transform2D,
)
class CT_GraphicalObject(BaseOxmlElement):
"""`a:graphic` element.
The container for the reference to or definition of the framed graphical object (table, chart,
etc.).
"""
graphicData: CT_GraphicalObjectData = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
"a:graphicData"
)
@property
def chart(self) -> CT_Chart | None:
"""The `c:chart` grandchild element, or |None| if not present."""
return self.graphicData.chart
class CT_GraphicalObjectData(BaseShapeElement):
"""`p:graphicData` element.
The direct container for a table, a chart, or another graphical object.
"""
chart: CT_Chart | None = ZeroOrOne("c:chart") # pyright: ignore[reportAssignmentType]
tbl: CT_Table | None = ZeroOrOne("a:tbl") # pyright: ignore[reportAssignmentType]
uri: str = RequiredAttribute("uri", XsdString) # pyright: ignore[reportAssignmentType]
@property
def blob_rId(self) -> str | None:
"""Optional `r:id` attribute value of `p:oleObj` descendent element.
This value is `None` when this `p:graphicData` element does not enclose an OLE object.
This value could also be `None` if an enclosed OLE object does not specify this attribute
(it is specified optional in the schema) but so far, all OLE objects we've encountered
specify this value.
"""
return None if self._oleObj is None else self._oleObj.rId
@property
def is_embedded_ole_obj(self) -> bool | None:
"""Optional boolean indicating an embedded OLE object.
Returns `None` when this `p:graphicData` element does not enclose an OLE object. `True`
indicates an embedded OLE object and `False` indicates a linked OLE object.
"""
return None if self._oleObj is None else self._oleObj.is_embedded
@property
def progId(self) -> str | None:
"""Optional str value of "progId" attribute of `p:oleObj` descendent.
This value identifies the "type" of the embedded object in terms of the application used
to open it.
This value is `None` when this `p:graphicData` element does not enclose an OLE object.
This could also be `None` if an enclosed OLE object does not specify this attribute (it is
specified optional in the schema) but so far, all OLE objects we've encountered specify
this value.
"""
return None if self._oleObj is None else self._oleObj.progId
@property
def showAsIcon(self) -> bool | None:
"""Optional value of "showAsIcon" attribute value of `p:oleObj` descendent.
This value is `None` when this `p:graphicData` element does not enclose an OLE object. It
is False when the `showAsIcon` attribute is omitted on the `p:oleObj` element.
"""
return None if self._oleObj is None else self._oleObj.showAsIcon
@property
def _oleObj(self) -> CT_OleObject | None:
"""Optional `p:oleObj` element contained in this `p:graphicData' element.
Returns `None` when this graphic-data element does not enclose an OLE object. Note that
this returns the last `p:oleObj` element found. There can be more than one `p:oleObj`
element because an `mc.AlternateContent` element may appear as the child of
`p:graphicData` and that alternate-content subtree can contain multiple compatibility
choices. The last one should suit best for reading purposes because it contains the lowest
common denominator.
"""
oleObjs = cast("list[CT_OleObject]", self.xpath(".//p:oleObj"))
return oleObjs[-1] if oleObjs else None
class CT_GraphicalObjectFrame(BaseShapeElement):
"""`p:graphicFrame` element.
A container for a table, a chart, or another graphical object.
"""
nvGraphicFramePr: CT_GraphicalObjectFrameNonVisual = ( # pyright: ignore[reportAssignmentType]
OneAndOnlyOne("p:nvGraphicFramePr")
)
xfrm: CT_Transform2D = OneAndOnlyOne("p:xfrm") # pyright: ignore
graphic: CT_GraphicalObject = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
"a:graphic"
)
@property
def chart(self) -> CT_Chart | None:
"""The `c:chart` great-grandchild element, or |None| if not present."""
return self.graphic.chart
@property
def chart_rId(self) -> str | None:
"""The `rId` attribute of the `c:chart` great-grandchild element.
|None| if not present.
"""
chart = self.chart
if chart is None:
return None
return chart.rId
def get_or_add_xfrm(self) -> CT_Transform2D:
"""Return the required `p:xfrm` child element.
Overrides version on BaseShapeElement.
"""
return self.xfrm
@property
def graphicData(self) -> CT_GraphicalObjectData:
"""`a:graphicData` grandchild of this graphic-frame element."""
return self.graphic.graphicData
@property
def graphicData_uri(self) -> str:
"""str value of `uri` attribute of `a:graphicData` grandchild."""
return self.graphic.graphicData.uri
@property
def has_oleobj(self) -> bool:
"""`True` for graphicFrame containing an OLE object, `False` otherwise."""
return self.graphicData.uri == GRAPHIC_DATA_URI_OLEOBJ
@property
def is_embedded_ole_obj(self) -> bool | None:
"""Optional boolean indicating an embedded OLE object.
Returns `None` when this `p:graphicFrame` element does not enclose an OLE object. `True`
indicates an embedded OLE object and `False` indicates a linked OLE object.
"""
return self.graphicData.is_embedded_ole_obj
@classmethod
def new_chart_graphicFrame(
cls, id_: int, name: str, rId: str, x: int, y: int, cx: int, cy: int
) -> CT_GraphicalObjectFrame:
"""Return a `p:graphicFrame` element tree populated with a chart element."""
graphicFrame = CT_GraphicalObjectFrame.new_graphicFrame(id_, name, x, y, cx, cy)
graphicData = graphicFrame.graphic.graphicData
graphicData.uri = GRAPHIC_DATA_URI_CHART
graphicData.append(CT_Chart.new_chart(rId))
return graphicFrame
@classmethod
def new_graphicFrame(
cls, id_: int, name: str, x: int, y: int, cx: int, cy: int
) -> CT_GraphicalObjectFrame:
"""Return a new `p:graphicFrame` element tree suitable for containing a table or chart.
Note that a graphicFrame element is not a valid shape until it contains a graphical object
such as a table.
"""
return cast(
CT_GraphicalObjectFrame,
parse_xml(
f"<p:graphicFrame {nsdecls('a', 'p')}>\n"
f" <p:nvGraphicFramePr>\n"
f' <p:cNvPr id="{id_}" name="{name}"/>\n'
f" <p:cNvGraphicFramePr>\n"
f' <a:graphicFrameLocks noGrp="1"/>\n'
f" </p:cNvGraphicFramePr>\n"
f" <p:nvPr/>\n"
f" </p:nvGraphicFramePr>\n"
f" <p:xfrm>\n"
f' <a:off x="{x}" y="{y}"/>\n'
f' <a:ext cx="{cx}" cy="{cy}"/>\n'
f" </p:xfrm>\n"
f" <a:graphic>\n"
f" <a:graphicData/>\n"
f" </a:graphic>\n"
f"</p:graphicFrame>"
),
)
@classmethod
def new_ole_object_graphicFrame(
cls,
id_: int,
name: str,
ole_object_rId: str,
progId: str,
icon_rId: str,
x: int,
y: int,
cx: int,
cy: int,
imgW: int,
imgH: int,
) -> CT_GraphicalObjectFrame:
"""Return newly-created `p:graphicFrame` for embedded OLE-object.
`ole_object_rId` identifies the relationship to the OLE-object part.
`progId` is a str identifying the object-type in terms of the application (program) used
to open it. This becomes an attribute of the same name in the `p:oleObj` element.
`icon_rId` identifies the relationship to an image part used to display the OLE-object as
an icon (vs. a preview).
"""
return cast(
CT_GraphicalObjectFrame,
parse_xml(
f"<p:graphicFrame {nsdecls('a', 'p', 'r')}>\n"
f" <p:nvGraphicFramePr>\n"
f' <p:cNvPr id="{id_}" name="{name}"/>\n'
f" <p:cNvGraphicFramePr>\n"
f' <a:graphicFrameLocks noGrp="1"/>\n'
f" </p:cNvGraphicFramePr>\n"
f" <p:nvPr/>\n"
f" </p:nvGraphicFramePr>\n"
f" <p:xfrm>\n"
f' <a:off x="{x}" y="{y}"/>\n'
f' <a:ext cx="{cx}" cy="{cy}"/>\n'
f" </p:xfrm>\n"
f" <a:graphic>\n"
f" <a:graphicData"
f' uri="http://schemas.openxmlformats.org/presentationml/2006/ole">\n'
f' <p:oleObj showAsIcon="1"'
f' r:id="{ole_object_rId}"'
f' imgW="{imgW}"'
f' imgH="{imgH}"'
f' progId="{progId}">\n'
f" <p:embed/>\n"
f" <p:pic>\n"
f" <p:nvPicPr>\n"
f' <p:cNvPr id="0" name=""/>\n'
f" <p:cNvPicPr/>\n"
f" <p:nvPr/>\n"
f" </p:nvPicPr>\n"
f" <p:blipFill>\n"
f' <a:blip r:embed="{icon_rId}"/>\n'
f" <a:stretch>\n"
f" <a:fillRect/>\n"
f" </a:stretch>\n"
f" </p:blipFill>\n"
f" <p:spPr>\n"
f" <a:xfrm>\n"
f' <a:off x="{x}" y="{y}"/>\n'
f' <a:ext cx="{cx}" cy="{cy}"/>\n'
f" </a:xfrm>\n"
f' <a:prstGeom prst="rect">\n'
f" <a:avLst/>\n"
f" </a:prstGeom>\n"
f" </p:spPr>\n"
f" </p:pic>\n"
f" </p:oleObj>\n"
f" </a:graphicData>\n"
f" </a:graphic>\n"
f"</p:graphicFrame>"
),
)
@classmethod
def new_table_graphicFrame(
cls, id_: int, name: str, rows: int, cols: int, x: int, y: int, cx: int, cy: int
) -> CT_GraphicalObjectFrame:
"""Return a `p:graphicFrame` element tree populated with a table element."""
graphicFrame = cls.new_graphicFrame(id_, name, x, y, cx, cy)
graphicFrame.graphic.graphicData.uri = GRAPHIC_DATA_URI_TABLE
graphicFrame.graphic.graphicData.append(CT_Table.new_tbl(rows, cols, cx, cy))
return graphicFrame
class CT_GraphicalObjectFrameNonVisual(BaseOxmlElement):
"""`p:nvGraphicFramePr` element.
This contains the non-visual properties of a graphic frame, such as name, id, etc.
"""
cNvPr: CT_NonVisualDrawingProps = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
"p:cNvPr"
)
nvPr: CT_ApplicationNonVisualDrawingProps = ( # pyright: ignore[reportAssignmentType]
OneAndOnlyOne("p:nvPr")
)
class CT_OleObject(BaseOxmlElement):
"""`p:oleObj` element, container for an OLE object (e.g. Excel file).
An OLE object can be either linked or embedded (hence the name).
"""
progId: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
"progId", XsdString
)
rId: str | None = OptionalAttribute("r:id", XsdString) # pyright: ignore[reportAssignmentType]
showAsIcon: bool = OptionalAttribute( # pyright: ignore[reportAssignmentType]
"showAsIcon", XsdBoolean, default=False
)
@property
def is_embedded(self) -> bool:
"""True when this OLE object is embedded, False when it is linked."""
return len(self.xpath("./p:embed")) > 0
|