1###############################################################################
2#
3# Relationships - A class for writing the Excel XLSX Worksheet file.
4#
5# SPDX-License-Identifier: BSD-2-Clause
6# Copyright 2013-2024, John McNamara, jmcnamara@cpan.org
7#
8
9# Package imports.
10from . import xmlwriter
11
12# Long namespace strings used in the class.
13schema_root = "http://schemas.openxmlformats.org"
14package_schema = schema_root + "/package/2006/relationships"
15document_schema = schema_root + "/officeDocument/2006/relationships"
16
17
18class Relationships(xmlwriter.XMLwriter):
19 """
20 A class for writing the Excel XLSX Relationships file.
21
22
23 """
24
25 ###########################################################################
26 #
27 # Public API.
28 #
29 ###########################################################################
30
31 def __init__(self):
32 """
33 Constructor.
34
35 """
36
37 super(Relationships, self).__init__()
38
39 self.relationships = []
40 self.id = 1
41
42 ###########################################################################
43 #
44 # Private API.
45 #
46 ###########################################################################
47
48 def _assemble_xml_file(self):
49 # Assemble and write the XML file.
50
51 # Write the XML declaration.
52 self._xml_declaration()
53
54 self._write_relationships()
55
56 # Close the file.
57 self._xml_close()
58
59 def _add_document_relationship(self, rel_type, target, target_mode=None):
60 # Add container relationship to XLSX .rels xml files.
61 rel_type = document_schema + rel_type
62
63 self.relationships.append((rel_type, target, target_mode))
64
65 def _add_package_relationship(self, rel_type, target):
66 # Add container relationship to XLSX .rels xml files.
67 rel_type = package_schema + rel_type
68
69 self.relationships.append((rel_type, target, None))
70
71 def _add_ms_package_relationship(self, rel_type, target):
72 # Add container relationship to XLSX .rels xml files. Uses MS schema.
73 schema = "http://schemas.microsoft.com/office/2006/relationships"
74 rel_type = schema + rel_type
75
76 self.relationships.append((rel_type, target, None))
77
78 def _add_rich_value_relationship(self):
79 # Add RichValue relationship to XLSX .rels xml files.
80 schema = "http://schemas.microsoft.com/office/2022/10/relationships/"
81 rel_type = schema + "richValueRel"
82 target = "richData/richValueRel.xml"
83 self.relationships.append((rel_type, target, None))
84
85 schema = "http://schemas.microsoft.com/office/2017/06/relationships/"
86 rel_type = schema + "rdRichValue"
87 target = "richData/rdrichvalue.xml"
88 self.relationships.append((rel_type, target, None))
89
90 schema = "http://schemas.microsoft.com/office/2017/06/relationships/"
91 rel_type = schema + "rdRichValueStructure"
92 target = "richData/rdrichvaluestructure.xml"
93 self.relationships.append((rel_type, target, None))
94
95 schema = "http://schemas.microsoft.com/office/2017/06/relationships/"
96 rel_type = schema + "rdRichValueTypes"
97 target = "richData/rdRichValueTypes.xml"
98 self.relationships.append((rel_type, target, None))
99
100 ###########################################################################
101 #
102 # XML methods.
103 #
104 ###########################################################################
105
106 def _write_relationships(self):
107 # Write the <Relationships> element.
108 attributes = [
109 (
110 "xmlns",
111 package_schema,
112 )
113 ]
114
115 self._xml_start_tag("Relationships", attributes)
116
117 for relationship in self.relationships:
118 self._write_relationship(relationship)
119
120 self._xml_end_tag("Relationships")
121
122 def _write_relationship(self, relationship):
123 # Write the <Relationship> element.
124 rel_type, target, target_mode = relationship
125
126 attributes = [
127 ("Id", "rId" + str(self.id)),
128 ("Type", rel_type),
129 ("Target", target),
130 ]
131
132 self.id += 1
133
134 if target_mode:
135 attributes.append(("TargetMode", target_mode))
136
137 self._xml_empty_tag("Relationship", attributes)