1###############################################################################
2#
3# Custom - A class for writing the Excel XLSX Custom Property file.
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
8#
9
10# Package imports.
11from typing import List, Tuple
12
13from . import xmlwriter
14
15
16class Custom(xmlwriter.XMLwriter):
17 """
18 A class for writing the Excel XLSX Custom Workbook Property file.
19
20
21 """
22
23 ###########################################################################
24 #
25 # Public API.
26 #
27 ###########################################################################
28
29 def __init__(self):
30 """
31 Constructor.
32
33 """
34
35 super().__init__()
36
37 self.properties = []
38 self.pid = 1
39
40 def _set_properties(self, properties: List[Tuple[str, str, str]]):
41 # Set the document properties.
42 self.properties = properties
43
44 ###########################################################################
45 #
46 # Private API.
47 #
48 ###########################################################################
49
50 def _assemble_xml_file(self):
51 # Assemble and write the XML file.
52
53 # Write the XML declaration.
54 self._xml_declaration()
55
56 self._write_properties()
57
58 self._xml_end_tag("Properties")
59
60 # Close the file.
61 self._xml_close()
62
63 ###########################################################################
64 #
65 # XML methods.
66 #
67 ###########################################################################
68
69 def _write_properties(self):
70 # Write the <Properties> element.
71 schema = "http://schemas.openxmlformats.org/officeDocument/2006/"
72 xmlns = schema + "custom-properties"
73 xmlns_vt = schema + "docPropsVTypes"
74
75 attributes = [
76 ("xmlns", xmlns),
77 ("xmlns:vt", xmlns_vt),
78 ]
79
80 self._xml_start_tag("Properties", attributes)
81
82 for custom_property in self.properties:
83 # Write the property element.
84 self._write_property(custom_property)
85
86 def _write_property(self, custom_property: Tuple[str, str, str]):
87 # Write the <property> element.
88
89 fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
90
91 name, value, property_type = custom_property
92 self.pid += 1
93
94 attributes = [
95 ("fmtid", fmtid),
96 ("pid", self.pid),
97 ("name", name),
98 ]
99
100 self._xml_start_tag("property", attributes)
101
102 if property_type == "number_int":
103 # Write the vt:i4 element.
104 self._write_vt_i4(value)
105 elif property_type == "number":
106 # Write the vt:r8 element.
107 self._write_vt_r8(value)
108 elif property_type == "date":
109 # Write the vt:filetime element.
110 self._write_vt_filetime(value)
111 elif property_type == "bool":
112 # Write the vt:bool element.
113 self._write_vt_bool(value)
114 else:
115 # Write the vt:lpwstr element.
116 self._write_vt_lpwstr(value)
117
118 self._xml_end_tag("property")
119
120 def _write_vt_lpwstr(self, value: str):
121 # Write the <vt:lpwstr> element.
122 self._xml_data_element("vt:lpwstr", value)
123
124 def _write_vt_filetime(self, value: str):
125 # Write the <vt:filetime> element.
126 self._xml_data_element("vt:filetime", value)
127
128 def _write_vt_i4(self, value: str):
129 # Write the <vt:i4> element.
130 self._xml_data_element("vt:i4", value)
131
132 def _write_vt_r8(self, value: str):
133 # Write the <vt:r8> element.
134 self._xml_data_element("vt:r8", value)
135
136 def _write_vt_bool(self, value: str):
137 # Write the <vt:bool> element.
138 self._xml_data_element("vt:bool", value)