Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/xlsxwriter/custom.py: 33%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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#
10# Package imports.
11from typing import List, Tuple
13from . import xmlwriter
16class Custom(xmlwriter.XMLwriter):
17 """
18 A class for writing the Excel XLSX Custom Workbook Property file.
21 """
23 ###########################################################################
24 #
25 # Public API.
26 #
27 ###########################################################################
29 def __init__(self) -> None:
30 """
31 Constructor.
33 """
35 super().__init__()
37 self.properties = []
38 self.pid = 1
40 def _set_properties(self, properties: List[Tuple[str, str, str]]) -> None:
41 # Set the document properties.
42 self.properties = properties
44 ###########################################################################
45 #
46 # Private API.
47 #
48 ###########################################################################
50 def _assemble_xml_file(self) -> None:
51 # Assemble and write the XML file.
53 # Write the XML declaration.
54 self._xml_declaration()
56 self._write_properties()
58 self._xml_end_tag("Properties")
60 # Close the file.
61 self._xml_close()
63 ###########################################################################
64 #
65 # XML methods.
66 #
67 ###########################################################################
69 def _write_properties(self) -> None:
70 # Write the <Properties> element.
71 schema = "http://schemas.openxmlformats.org/officeDocument/2006/"
72 xmlns = schema + "custom-properties"
73 xmlns_vt = schema + "docPropsVTypes"
75 attributes = [
76 ("xmlns", xmlns),
77 ("xmlns:vt", xmlns_vt),
78 ]
80 self._xml_start_tag("Properties", attributes)
82 for custom_property in self.properties:
83 # Write the property element.
84 self._write_property(custom_property)
86 def _write_property(self, custom_property: Tuple[str, str, str]) -> None:
87 # Write the <property> element.
89 fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
91 name, value, property_type = custom_property
92 self.pid += 1
94 attributes = [
95 ("fmtid", fmtid),
96 ("pid", self.pid),
97 ("name", name),
98 ]
100 self._xml_start_tag("property", attributes)
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)
118 self._xml_end_tag("property")
120 def _write_vt_lpwstr(self, value: str) -> None:
121 # Write the <vt:lpwstr> element.
122 self._xml_data_element("vt:lpwstr", value)
124 def _write_vt_filetime(self, value: str) -> None:
125 # Write the <vt:filetime> element.
126 self._xml_data_element("vt:filetime", value)
128 def _write_vt_i4(self, value: str) -> None:
129 # Write the <vt:i4> element.
130 self._xml_data_element("vt:i4", value)
132 def _write_vt_r8(self, value: str) -> None:
133 # Write the <vt:r8> element.
134 self._xml_data_element("vt:r8", value)
136 def _write_vt_bool(self, value: str) -> None:
137 # Write the <vt:bool> element.
138 self._xml_data_element("vt:bool", value)