Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/xlsxwriter/app.py: 98%
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# App - A class for writing the Excel XLSX App 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 App(xmlwriter.XMLwriter):
14 """
15 A class for writing the Excel XLSX App file.
18 """
20 ###########################################################################
21 #
22 # Public API.
23 #
24 ###########################################################################
26 def __init__(self):
27 """
28 Constructor.
30 """
32 super(App, self).__init__()
34 self.part_names = []
35 self.heading_pairs = []
36 self.properties = {}
37 self.doc_security = 0
39 def _add_part_name(self, part_name):
40 # Add the name of a workbook Part such as 'Sheet1' or 'Print_Titles'.
41 self.part_names.append(part_name)
43 def _add_heading_pair(self, heading_pair):
44 # Add the name of a workbook Heading Pair such as 'Worksheets',
45 # 'Charts' or 'Named Ranges'.
47 # Ignore empty pairs such as chartsheets.
48 if not heading_pair[1]:
49 return
51 self.heading_pairs.append(("lpstr", heading_pair[0]))
52 self.heading_pairs.append(("i4", heading_pair[1]))
54 def _set_properties(self, properties):
55 # Set the document properties.
56 self.properties = properties
58 ###########################################################################
59 #
60 # Private API.
61 #
62 ###########################################################################
64 def _assemble_xml_file(self):
65 # Assemble and write the XML file.
67 # Write the XML declaration.
68 self._xml_declaration()
70 self._write_properties()
71 self._write_application()
72 self._write_doc_security()
73 self._write_scale_crop()
74 self._write_heading_pairs()
75 self._write_titles_of_parts()
76 self._write_manager()
77 self._write_company()
78 self._write_links_up_to_date()
79 self._write_shared_doc()
80 self._write_hyperlink_base()
81 self._write_hyperlinks_changed()
82 self._write_app_version()
84 self._xml_end_tag("Properties")
86 # Close the file.
87 self._xml_close()
89 ###########################################################################
90 #
91 # XML methods.
92 #
93 ###########################################################################
95 def _write_properties(self):
96 # Write the <Properties> element.
97 schema = "http://schemas.openxmlformats.org/officeDocument/2006/"
98 xmlns = schema + "extended-properties"
99 xmlns_vt = schema + "docPropsVTypes"
101 attributes = [
102 ("xmlns", xmlns),
103 ("xmlns:vt", xmlns_vt),
104 ]
106 self._xml_start_tag("Properties", attributes)
108 def _write_application(self):
109 # Write the <Application> element.
110 self._xml_data_element("Application", "Microsoft Excel")
112 def _write_doc_security(self):
113 # Write the <DocSecurity> element.
114 self._xml_data_element("DocSecurity", self.doc_security)
116 def _write_scale_crop(self):
117 # Write the <ScaleCrop> element.
118 self._xml_data_element("ScaleCrop", "false")
120 def _write_heading_pairs(self):
121 # Write the <HeadingPairs> element.
122 self._xml_start_tag("HeadingPairs")
123 self._write_vt_vector("variant", self.heading_pairs)
124 self._xml_end_tag("HeadingPairs")
126 def _write_titles_of_parts(self):
127 # Write the <TitlesOfParts> element.
128 parts_data = []
130 self._xml_start_tag("TitlesOfParts")
132 for part_name in self.part_names:
133 parts_data.append(("lpstr", part_name))
135 self._write_vt_vector("lpstr", parts_data)
137 self._xml_end_tag("TitlesOfParts")
139 def _write_vt_vector(self, base_type, vector_data):
140 # Write the <vt:vector> element.
141 attributes = [
142 ("size", len(vector_data)),
143 ("baseType", base_type),
144 ]
146 self._xml_start_tag("vt:vector", attributes)
148 for vt_data in vector_data:
149 if base_type == "variant":
150 self._xml_start_tag("vt:variant")
152 self._write_vt_data(vt_data)
154 if base_type == "variant":
155 self._xml_end_tag("vt:variant")
157 self._xml_end_tag("vt:vector")
159 def _write_vt_data(self, vt_data):
160 # Write the <vt:*> elements such as <vt:lpstr> and <vt:if>.
161 self._xml_data_element("vt:%s" % vt_data[0], vt_data[1])
163 def _write_company(self):
164 company = self.properties.get("company", "")
166 self._xml_data_element("Company", company)
168 def _write_manager(self):
169 # Write the <Manager> element.
170 if "manager" not in self.properties:
171 return
173 self._xml_data_element("Manager", self.properties["manager"])
175 def _write_links_up_to_date(self):
176 # Write the <LinksUpToDate> element.
177 self._xml_data_element("LinksUpToDate", "false")
179 def _write_shared_doc(self):
180 # Write the <SharedDoc> element.
181 self._xml_data_element("SharedDoc", "false")
183 def _write_hyperlink_base(self):
184 # Write the <HyperlinkBase> element.
185 hyperlink_base = self.properties.get("hyperlink_base")
187 if hyperlink_base is None:
188 return
190 self._xml_data_element("HyperlinkBase", hyperlink_base)
192 def _write_hyperlinks_changed(self):
193 # Write the <HyperlinksChanged> element.
194 self._xml_data_element("HyperlinksChanged", "false")
196 def _write_app_version(self):
197 # Write the <AppVersion> element.
198 self._xml_data_element("AppVersion", "12.0000")