Source code

Download source archive ↓

src/openpyxl/drawing/picture.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.xml.constants import DRAWING_NS
4
5from openpyxl.descriptors.serialisable import Serialisable
6from openpyxl.descriptors import (
7    Typed,
8    Bool,
9    String,
10    Alias,
11)
12from openpyxl.descriptors.excel import ExtensionList as OfficeArtExtensionList
13
14from openpyxl.chart.shapes import GraphicalProperties
15
16from .fill import BlipFillProperties
17from .properties import NonVisualDrawingProps
18from .geometry import ShapeStyle
19
20
21class PictureLocking(Serialisable):
22
23    tagname = "picLocks"
24    namespace = DRAWING_NS
25
26    # Using attribute group AG_Locking
27    noCrop = Bool(allow_none=True)
28    noGrp = Bool(allow_none=True)
29    noSelect = Bool(allow_none=True)
30    noRot = Bool(allow_none=True)
31    noChangeAspect = Bool(allow_none=True)
32    noMove = Bool(allow_none=True)
33    noResize = Bool(allow_none=True)
34    noEditPoints = Bool(allow_none=True)
35    noAdjustHandles = Bool(allow_none=True)
36    noChangeArrowheads = Bool(allow_none=True)
37    noChangeShapeType = Bool(allow_none=True)
38    extLst = Typed(expected_type=OfficeArtExtensionList, allow_none=True)
39
40    __elements__ = ()
41
42    def __init__(self,
43                 noCrop=None,
44                 noGrp=None,
45                 noSelect=None,
46                 noRot=None,
47                 noChangeAspect=None,
48                 noMove=None,
49                 noResize=None,
50                 noEditPoints=None,
51                 noAdjustHandles=None,
52                 noChangeArrowheads=None,
53                 noChangeShapeType=None,
54                 extLst=None,
55                ):
56        self.noCrop = noCrop
57        self.noGrp = noGrp
58        self.noSelect = noSelect
59        self.noRot = noRot
60        self.noChangeAspect = noChangeAspect
61        self.noMove = noMove
62        self.noResize = noResize
63        self.noEditPoints = noEditPoints
64        self.noAdjustHandles = noAdjustHandles
65        self.noChangeArrowheads = noChangeArrowheads
66        self.noChangeShapeType = noChangeShapeType
67
68
69class NonVisualPictureProperties(Serialisable):
70
71    tagname = "cNvPicPr"
72
73    preferRelativeResize = Bool(allow_none=True)
74    picLocks = Typed(expected_type=PictureLocking, allow_none=True)
75    extLst = Typed(expected_type=OfficeArtExtensionList, allow_none=True)
76
77    __elements__ = ("picLocks",)
78
79    def __init__(self,
80                 preferRelativeResize=None,
81                 picLocks=None,
82                 extLst=None,
83                ):
84        self.preferRelativeResize = preferRelativeResize
85        self.picLocks = picLocks
86
87
88class PictureNonVisual(Serialisable):
89
90    tagname = "nvPicPr"
91
92    cNvPr = Typed(expected_type=NonVisualDrawingProps, )
93    cNvPicPr = Typed(expected_type=NonVisualPictureProperties, )
94
95    __elements__ = ("cNvPr", "cNvPicPr")
96
97    def __init__(self,
98                 cNvPr=None,
99                 cNvPicPr=None,
100                ):
101        if cNvPr is None:
102            cNvPr = NonVisualDrawingProps(id=0, name="Image 1", descr="Name of file")
103        self.cNvPr = cNvPr
104        if cNvPicPr is None:
105            cNvPicPr = NonVisualPictureProperties()
106        self.cNvPicPr = cNvPicPr
107
108
109
110
111class PictureFrame(Serialisable):
112
113    tagname = "pic"
114
115    macro = String(allow_none=True)
116    fPublished = Bool(allow_none=True)
117    nvPicPr = Typed(expected_type=PictureNonVisual, )
118    blipFill = Typed(expected_type=BlipFillProperties, )
119    spPr = Typed(expected_type=GraphicalProperties, )
120    graphicalProperties = Alias('spPr')
121    style = Typed(expected_type=ShapeStyle, allow_none=True)
122
123    __elements__ = ("nvPicPr", "blipFill", "spPr", "style")
124
125    def __init__(self,
126                 macro=None,
127                 fPublished=None,
128                 nvPicPr=None,
129                 blipFill=None,
130                 spPr=None,
131                 style=None,
132                ):
133        self.macro = macro
134        self.fPublished = fPublished
135        if nvPicPr is None:
136            nvPicPr = PictureNonVisual()
137        self.nvPicPr = nvPicPr
138        if blipFill is None:
139            blipFill = BlipFillProperties()
140        self.blipFill = blipFill
141        if spPr is None:
142            spPr = GraphicalProperties()
143        self.spPr = spPr
144        self.style = style