1###############################################################################
2#
3# RichValue - A class for writing the Excel XLSX rdrichvalue.xml file.
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
8#
9
10from xlsxwriter.image import Image
11
12from . import xmlwriter
13
14
15class RichValue(xmlwriter.XMLwriter):
16 """
17 A class for writing the Excel XLSX rdrichvalue.xml file.
18
19
20 """
21
22 ###########################################################################
23 #
24 # Public API.
25 #
26 ###########################################################################
27
28 def __init__(self):
29 """
30 Constructor.
31
32 """
33
34 super().__init__()
35 self.embedded_images = []
36
37 ###########################################################################
38 #
39 # Private API.
40 #
41 ###########################################################################
42
43 def _assemble_xml_file(self):
44 # Assemble and write the XML file.
45
46 # Write the XML declaration.
47 self._xml_declaration()
48
49 # Write the rvData element.
50 self._write_rv_data()
51
52 self._xml_end_tag("rvData")
53
54 # Close the file.
55 self._xml_close()
56
57 ###########################################################################
58 #
59 # XML methods.
60 #
61 ###########################################################################
62 def _write_rv_data(self):
63 # Write the <rvData> element.
64 xmlns = "http://schemas.microsoft.com/office/spreadsheetml/2017/richdata"
65
66 attributes = [
67 ("xmlns", xmlns),
68 ("count", len(self.embedded_images)),
69 ]
70
71 self._xml_start_tag("rvData", attributes)
72
73 for index, image in enumerate(self.embedded_images):
74 # Write the rv element.
75 self._write_rv(index, image)
76
77 def _write_rv(self, index, image: Image):
78 # Write the <rv> element.
79 attributes = [("s", 0)]
80 value = 5
81
82 if image.decorative:
83 value = 6
84
85 self._xml_start_tag("rv", attributes)
86
87 # Write the v elements.
88 self._write_v(index)
89 self._write_v(value)
90
91 if image.description:
92 self._write_v(image.description)
93
94 self._xml_end_tag("rv")
95
96 def _write_v(self, data):
97 # Write the <v> element.
98 self._xml_data_element("v", data)