Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/xlsxwriter/custom.py: 30%
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# Copyright 2013-2024, John McNamara, jmcnamara@cpan.org
7#
9# Package imports.
10from . import xmlwriter
13class Custom(xmlwriter.XMLwriter):
14 """
15 A class for writing the Excel XLSX Custom Workbook Property file.
18 """
20 ###########################################################################
21 #
22 # Public API.
23 #
24 ###########################################################################
26 def __init__(self):
27 """
28 Constructor.
30 """
32 super(Custom, self).__init__()
34 self.properties = []
35 self.pid = 1
37 def _set_properties(self, properties):
38 # Set the document properties.
39 self.properties = properties
41 ###########################################################################
42 #
43 # Private API.
44 #
45 ###########################################################################
47 def _assemble_xml_file(self):
48 # Assemble and write the XML file.
50 # Write the XML declaration.
51 self._xml_declaration()
53 self._write_properties()
55 self._xml_end_tag("Properties")
57 # Close the file.
58 self._xml_close()
60 ###########################################################################
61 #
62 # XML methods.
63 #
64 ###########################################################################
66 def _write_properties(self):
67 # Write the <Properties> element.
68 schema = "http://schemas.openxmlformats.org/officeDocument/2006/"
69 xmlns = schema + "custom-properties"
70 xmlns_vt = schema + "docPropsVTypes"
72 attributes = [
73 ("xmlns", xmlns),
74 ("xmlns:vt", xmlns_vt),
75 ]
77 self._xml_start_tag("Properties", attributes)
79 for custom_property in self.properties:
80 # Write the property element.
81 self._write_property(custom_property)
83 def _write_property(self, custom_property):
84 # Write the <property> element.
86 fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
88 name, value, property_type = custom_property
89 self.pid += 1
91 attributes = [
92 ("fmtid", fmtid),
93 ("pid", self.pid),
94 ("name", name),
95 ]
97 self._xml_start_tag("property", attributes)
99 if property_type == "number_int":
100 # Write the vt:i4 element.
101 self._write_vt_i4(value)
102 elif property_type == "number":
103 # Write the vt:r8 element.
104 self._write_vt_r8(value)
105 elif property_type == "date":
106 # Write the vt:filetime element.
107 self._write_vt_filetime(value)
108 elif property_type == "bool":
109 # Write the vt:bool element.
110 self._write_vt_bool(value)
111 else:
112 # Write the vt:lpwstr element.
113 self._write_vt_lpwstr(value)
115 self._xml_end_tag("property")
117 def _write_vt_lpwstr(self, value):
118 # Write the <vt:lpwstr> element.
119 self._xml_data_element("vt:lpwstr", value)
121 def _write_vt_filetime(self, value):
122 # Write the <vt:filetime> element.
123 self._xml_data_element("vt:filetime", value)
125 def _write_vt_i4(self, value):
126 # Write the <vt:i4> element.
127 self._xml_data_element("vt:i4", value)
129 def _write_vt_r8(self, value):
130 # Write the <vt:r8> element.
131 self._xml_data_element("vt:r8", value)
133 def _write_vt_bool(self, value):
134 # Write the <vt:bool> element.
136 if value:
137 value = "true"
138 else:
139 value = "false"
141 self._xml_data_element("vt:bool", value)