Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/xlsxwriter/core.py: 84%

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

83 statements  

1############################################################################### 

2# 

3# Core - 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# Standard packages. 

10from datetime import datetime, timezone 

11 

12# Package imports. 

13from . import xmlwriter 

14 

15 

16class Core(xmlwriter.XMLwriter): 

17 """ 

18 A class for writing the Excel XLSX Core file. 

19 

20 

21 """ 

22 

23 ########################################################################### 

24 # 

25 # Public API. 

26 # 

27 ########################################################################### 

28 

29 def __init__(self): 

30 """ 

31 Constructor. 

32 

33 """ 

34 

35 super(Core, self).__init__() 

36 

37 self.properties = {} 

38 

39 ########################################################################### 

40 # 

41 # Private API. 

42 # 

43 ########################################################################### 

44 

45 def _assemble_xml_file(self): 

46 # Assemble and write the XML file. 

47 

48 # Write the XML declaration. 

49 self._xml_declaration() 

50 

51 self._write_cp_core_properties() 

52 self._write_dc_title() 

53 self._write_dc_subject() 

54 self._write_dc_creator() 

55 self._write_cp_keywords() 

56 self._write_dc_description() 

57 self._write_cp_last_modified_by() 

58 self._write_dcterms_created() 

59 self._write_dcterms_modified() 

60 self._write_cp_category() 

61 self._write_cp_content_status() 

62 

63 self._xml_end_tag("cp:coreProperties") 

64 

65 # Close the file. 

66 self._xml_close() 

67 

68 def _set_properties(self, properties): 

69 # Set the document properties. 

70 self.properties = properties 

71 

72 def _datetime_to_iso8601_date(self, date): 

73 # Convert to a ISO 8601 style "2010-01-01T00:00:00Z" date. 

74 if not date: 

75 date = datetime.now(timezone.utc) 

76 

77 return date.strftime("%Y-%m-%dT%H:%M:%SZ") 

78 

79 ########################################################################### 

80 # 

81 # XML methods. 

82 # 

83 ########################################################################### 

84 

85 def _write_cp_core_properties(self): 

86 # Write the <cp:coreProperties> element. 

87 

88 xmlns_cp = ( 

89 "http://schemas.openxmlformats.org/package/2006/" 

90 + "metadata/core-properties" 

91 ) 

92 xmlns_dc = "http://purl.org/dc/elements/1.1/" 

93 xmlns_dcterms = "http://purl.org/dc/terms/" 

94 xmlns_dcmitype = "http://purl.org/dc/dcmitype/" 

95 xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance" 

96 

97 attributes = [ 

98 ("xmlns:cp", xmlns_cp), 

99 ("xmlns:dc", xmlns_dc), 

100 ("xmlns:dcterms", xmlns_dcterms), 

101 ("xmlns:dcmitype", xmlns_dcmitype), 

102 ("xmlns:xsi", xmlns_xsi), 

103 ] 

104 

105 self._xml_start_tag("cp:coreProperties", attributes) 

106 

107 def _write_dc_creator(self): 

108 # Write the <dc:creator> element. 

109 data = self.properties.get("author", "") 

110 

111 self._xml_data_element("dc:creator", data) 

112 

113 def _write_cp_last_modified_by(self): 

114 # Write the <cp:lastModifiedBy> element. 

115 data = self.properties.get("author", "") 

116 

117 self._xml_data_element("cp:lastModifiedBy", data) 

118 

119 def _write_dcterms_created(self): 

120 # Write the <dcterms:created> element. 

121 date = self.properties.get("created", datetime.now(timezone.utc)) 

122 

123 xsi_type = "dcterms:W3CDTF" 

124 

125 date = self._datetime_to_iso8601_date(date) 

126 

127 attributes = [ 

128 ( 

129 "xsi:type", 

130 xsi_type, 

131 ) 

132 ] 

133 

134 self._xml_data_element("dcterms:created", date, attributes) 

135 

136 def _write_dcterms_modified(self): 

137 # Write the <dcterms:modified> element. 

138 date = self.properties.get("created", datetime.now(timezone.utc)) 

139 

140 xsi_type = "dcterms:W3CDTF" 

141 

142 date = self._datetime_to_iso8601_date(date) 

143 

144 attributes = [ 

145 ( 

146 "xsi:type", 

147 xsi_type, 

148 ) 

149 ] 

150 

151 self._xml_data_element("dcterms:modified", date, attributes) 

152 

153 def _write_dc_title(self): 

154 # Write the <dc:title> element. 

155 if "title" in self.properties: 

156 data = self.properties["title"] 

157 else: 

158 return 

159 

160 self._xml_data_element("dc:title", data) 

161 

162 def _write_dc_subject(self): 

163 # Write the <dc:subject> element. 

164 if "subject" in self.properties: 

165 data = self.properties["subject"] 

166 else: 

167 return 

168 

169 self._xml_data_element("dc:subject", data) 

170 

171 def _write_cp_keywords(self): 

172 # Write the <cp:keywords> element. 

173 if "keywords" in self.properties: 

174 data = self.properties["keywords"] 

175 else: 

176 return 

177 

178 self._xml_data_element("cp:keywords", data) 

179 

180 def _write_dc_description(self): 

181 # Write the <dc:description> element. 

182 if "comments" in self.properties: 

183 data = self.properties["comments"] 

184 else: 

185 return 

186 

187 self._xml_data_element("dc:description", data) 

188 

189 def _write_cp_category(self): 

190 # Write the <cp:category> element. 

191 if "category" in self.properties: 

192 data = self.properties["category"] 

193 else: 

194 return 

195 

196 self._xml_data_element("cp:category", data) 

197 

198 def _write_cp_content_status(self): 

199 # Write the <cp:contentStatus> element. 

200 if "status" in self.properties: 

201 data = self.properties["status"] 

202 else: 

203 return 

204 

205 self._xml_data_element("cp:contentStatus", data)