hyb
2025-11-04 668edf874b4f77214a8ff4513e60e3c1a973f532
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
# Copyright (c) 2010-2024 openpyxl
 
"""Implementation of custom properties see § 22.3 in the specification"""
 
 
from warnings import warn
 
from openpyxl.descriptors import Strict
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors.sequence import Sequence
from openpyxl.descriptors import (
    Alias,
    String,
    Integer,
    Float,
    DateTime,
    Bool,
)
from openpyxl.descriptors.nested import (
    NestedText,
)
 
from openpyxl.xml.constants import (
    CUSTPROPS_NS,
    VTYPES_NS,
    CPROPS_FMTID,
)
 
from .core import NestedDateTime
 
 
class NestedBoolText(Bool, NestedText):
    """
    Descriptor for handling nested elements with the value stored in the text part
    """
 
    pass
 
 
class _CustomDocumentProperty(Serialisable):
 
    """
    Low-level representation of a Custom Document Property.
    Not used directly
    Must always contain a child element, even if this is empty
    """
 
    tagname = "property"
    _typ = None
 
    name = String(allow_none=True)
    lpwstr = NestedText(expected_type=str, allow_none=True, namespace=VTYPES_NS)
    i4 = NestedText(expected_type=int, allow_none=True, namespace=VTYPES_NS)
    r8 = NestedText(expected_type=float, allow_none=True, namespace=VTYPES_NS)
    filetime = NestedDateTime(allow_none=True, namespace=VTYPES_NS)
    bool = NestedBoolText(expected_type=bool, allow_none=True, namespace=VTYPES_NS)
    linkTarget = String(expected_type=str, allow_none=True)
    fmtid = String()
    pid = Integer()
 
    def __init__(self,
                 name=None,
                 pid=0,
                 fmtid=CPROPS_FMTID,
                 linkTarget=None,
                 **kw):
        self.fmtid = fmtid
        self.pid = pid
        self.name = name
        self._typ = None
        self.linkTarget = linkTarget
 
        for k, v in kw.items():
            setattr(self, k, v)
            setattr(self, "_typ", k) # ugh!
        for e in self.__elements__:
            if e not in kw:
                setattr(self, e, None)
 
 
    @property
    def type(self):
        if self._typ is not None:
            return self._typ
        for a in self.__elements__:
            if getattr(self, a) is not None:
                return a
        if self.linkTarget is not None:
            return "linkTarget"
 
 
    def to_tree(self, tagname=None, idx=None, namespace=None):
        child = getattr(self, self._typ, None)
        if child is None:
            setattr(self, self._typ, "")
 
        return super().to_tree(tagname=None, idx=None, namespace=None)
 
 
class _CustomDocumentPropertyList(Serialisable):
 
    """
    Parses and seriliases property lists but is not used directly
    """
 
    tagname = "Properties"
 
    property = Sequence(expected_type=_CustomDocumentProperty, namespace=CUSTPROPS_NS)
    customProps = Alias("property")
 
 
    def __init__(self, property=()):
        self.property = property
 
 
    def __len__(self):
        return len(self.property)
 
 
    def to_tree(self, tagname=None, idx=None, namespace=None):
        for idx, p in enumerate(self.property, 2):
            p.pid = idx
        tree = super().to_tree(tagname, idx, namespace)
        tree.set("xmlns", CUSTPROPS_NS)
 
        return tree
 
 
class _TypedProperty(Strict):
 
    name = String()
 
    def __init__(self,
                 name,
                 value):
        self.name = name
        self.value = value
 
 
    def __eq__(self, other):
        return self.name == other.name and self.value == other.value
 
 
    def __repr__(self):
        return f"{self.__class__.__name__}, name={self.name}, value={self.value}"
 
 
class IntProperty(_TypedProperty):
 
    value = Integer()
 
 
class FloatProperty(_TypedProperty):
 
    value = Float()
 
 
class StringProperty(_TypedProperty):
 
    value = String(allow_none=True)
 
 
class DateTimeProperty(_TypedProperty):
 
    value = DateTime()
 
 
class BoolProperty(_TypedProperty):
 
    value = Bool()
 
 
class LinkProperty(_TypedProperty):
 
    value = String()
 
 
# from Python
CLASS_MAPPING = {
    StringProperty: "lpwstr",
    IntProperty: "i4",
    FloatProperty: "r8",
    DateTimeProperty: "filetime",
    BoolProperty: "bool",
    LinkProperty: "linkTarget"
}
 
XML_MAPPING = {v:k for k,v in CLASS_MAPPING.items()}
 
 
class CustomPropertyList(Strict):
 
 
    props = Sequence(expected_type=_TypedProperty)
 
    def __init__(self):
        self.props = []
 
 
    @classmethod
    def from_tree(cls, tree):
        """
        Create list from OOXML element
        """
        prop_list = _CustomDocumentPropertyList.from_tree(tree)
        props = []
 
        for prop in prop_list.property:
            attr = prop.type
 
            typ = XML_MAPPING.get(attr, None)
            if not typ:
                warn(f"Unknown type for {prop.name}")
                continue
            value = getattr(prop, attr)
            link = prop.linkTarget
            if link is not None:
                typ = LinkProperty
                value = prop.linkTarget
 
            new_prop = typ(name=prop.name, value=value)
            props.append(new_prop)
 
        new_prop_list = cls()
        new_prop_list.props = props
        return new_prop_list
 
 
    def append(self, prop):
        if prop.name in self.names:
            raise ValueError(f"Property with name {prop.name} already exists")
 
        self.props.append(prop)
 
 
    def to_tree(self):
        props = []
 
        for p in self.props:
            attr = CLASS_MAPPING.get(p.__class__, None)
            if not attr:
                raise TypeError("Unknown adapter for {p}")
            np = _CustomDocumentProperty(name=p.name, **{attr:p.value})
            if isinstance(p, LinkProperty):
                np._typ = "lpwstr"
                #np.lpwstr = ""
            props.append(np)
 
        prop_list = _CustomDocumentPropertyList(property=props)
        return prop_list.to_tree()
 
 
    def __len__(self):
        return len(self.props)
 
 
    @property
    def names(self):
        """List of property names"""
        return [p.name for p in self.props]
 
 
    def __getitem__(self, name):
        """
        Get property by name
        """
        for p in self.props:
            if p.name == name:
                return p
        raise KeyError(f"Property with name {name} not found")
 
 
    def __delitem__(self, name):
        """
        Delete a propery by name
        """
        for idx, p in enumerate(self.props):
            if p.name == name:
                self.props.pop(idx)
                return
        raise KeyError(f"Property with name {name} not found")
 
 
    def __repr__(self):
        return f"{self.__class__.__name__} containing {self.props}"
 
 
    def __iter__(self):
        return iter(self.props)