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

89 statements  

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# 

8 

9# Package imports. 

10from . import xmlwriter 

11 

12 

13class App(xmlwriter.XMLwriter): 

14 """ 

15 A class for writing the Excel XLSX App file. 

16 

17 

18 """ 

19 

20 ########################################################################### 

21 # 

22 # Public API. 

23 # 

24 ########################################################################### 

25 

26 def __init__(self): 

27 """ 

28 Constructor. 

29 

30 """ 

31 

32 super(App, self).__init__() 

33 

34 self.part_names = [] 

35 self.heading_pairs = [] 

36 self.properties = {} 

37 self.doc_security = 0 

38 

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) 

42 

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'. 

46 

47 # Ignore empty pairs such as chartsheets. 

48 if not heading_pair[1]: 

49 return 

50 

51 self.heading_pairs.append(("lpstr", heading_pair[0])) 

52 self.heading_pairs.append(("i4", heading_pair[1])) 

53 

54 def _set_properties(self, properties): 

55 # Set the document properties. 

56 self.properties = properties 

57 

58 ########################################################################### 

59 # 

60 # Private API. 

61 # 

62 ########################################################################### 

63 

64 def _assemble_xml_file(self): 

65 # Assemble and write the XML file. 

66 

67 # Write the XML declaration. 

68 self._xml_declaration() 

69 

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() 

83 

84 self._xml_end_tag("Properties") 

85 

86 # Close the file. 

87 self._xml_close() 

88 

89 ########################################################################### 

90 # 

91 # XML methods. 

92 # 

93 ########################################################################### 

94 

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" 

100 

101 attributes = [ 

102 ("xmlns", xmlns), 

103 ("xmlns:vt", xmlns_vt), 

104 ] 

105 

106 self._xml_start_tag("Properties", attributes) 

107 

108 def _write_application(self): 

109 # Write the <Application> element. 

110 self._xml_data_element("Application", "Microsoft Excel") 

111 

112 def _write_doc_security(self): 

113 # Write the <DocSecurity> element. 

114 self._xml_data_element("DocSecurity", self.doc_security) 

115 

116 def _write_scale_crop(self): 

117 # Write the <ScaleCrop> element. 

118 self._xml_data_element("ScaleCrop", "false") 

119 

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") 

125 

126 def _write_titles_of_parts(self): 

127 # Write the <TitlesOfParts> element. 

128 parts_data = [] 

129 

130 self._xml_start_tag("TitlesOfParts") 

131 

132 for part_name in self.part_names: 

133 parts_data.append(("lpstr", part_name)) 

134 

135 self._write_vt_vector("lpstr", parts_data) 

136 

137 self._xml_end_tag("TitlesOfParts") 

138 

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 ] 

145 

146 self._xml_start_tag("vt:vector", attributes) 

147 

148 for vt_data in vector_data: 

149 if base_type == "variant": 

150 self._xml_start_tag("vt:variant") 

151 

152 self._write_vt_data(vt_data) 

153 

154 if base_type == "variant": 

155 self._xml_end_tag("vt:variant") 

156 

157 self._xml_end_tag("vt:vector") 

158 

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]) 

162 

163 def _write_company(self): 

164 company = self.properties.get("company", "") 

165 

166 self._xml_data_element("Company", company) 

167 

168 def _write_manager(self): 

169 # Write the <Manager> element. 

170 if "manager" not in self.properties: 

171 return 

172 

173 self._xml_data_element("Manager", self.properties["manager"]) 

174 

175 def _write_links_up_to_date(self): 

176 # Write the <LinksUpToDate> element. 

177 self._xml_data_element("LinksUpToDate", "false") 

178 

179 def _write_shared_doc(self): 

180 # Write the <SharedDoc> element. 

181 self._xml_data_element("SharedDoc", "false") 

182 

183 def _write_hyperlink_base(self): 

184 # Write the <HyperlinkBase> element. 

185 hyperlink_base = self.properties.get("hyperlink_base") 

186 

187 if hyperlink_base is None: 

188 return 

189 

190 self._xml_data_element("HyperlinkBase", hyperlink_base) 

191 

192 def _write_hyperlinks_changed(self): 

193 # Write the <HyperlinksChanged> element. 

194 self._xml_data_element("HyperlinksChanged", "false") 

195 

196 def _write_app_version(self): 

197 # Write the <AppVersion> element. 

198 self._xml_data_element("AppVersion", "12.0000")