Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/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

80 statements  

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

2# 

3# Core - A class for writing the Excel XLSX Worksheet file. 

4# 

5# SPDX-License-Identifier: BSD-2-Clause 

6# 

7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org 

8# 

9 

10from datetime import datetime, timezone 

11from typing import Dict, Union 

12 

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

36 

37 self.properties = {} 

38 self.iso_date = "" 

39 

40 ########################################################################### 

41 # 

42 # Private API. 

43 # 

44 ########################################################################### 

45 

46 def _assemble_xml_file(self): 

47 # Assemble and write the XML file. 

48 

49 # Set the creation date for the file. 

50 date = self.properties.get("created") 

51 if not isinstance(date, datetime): 

52 date = datetime.now(timezone.utc) 

53 

54 self.iso_date = date.strftime("%Y-%m-%dT%H:%M:%SZ") 

55 

56 # Write the XML declaration. 

57 self._xml_declaration() 

58 

59 self._write_cp_core_properties() 

60 self._write_dc_title() 

61 self._write_dc_subject() 

62 self._write_dc_creator() 

63 self._write_cp_keywords() 

64 self._write_dc_description() 

65 self._write_cp_last_modified_by() 

66 self._write_dcterms_created() 

67 self._write_dcterms_modified() 

68 self._write_cp_category() 

69 self._write_cp_content_status() 

70 

71 self._xml_end_tag("cp:coreProperties") 

72 

73 # Close the file. 

74 self._xml_close() 

75 

76 def _set_properties(self, properties: Dict[str, Union[str, datetime]]): 

77 # Set the document properties. 

78 self.properties = properties 

79 

80 ########################################################################### 

81 # 

82 # XML methods. 

83 # 

84 ########################################################################### 

85 

86 def _write_cp_core_properties(self): 

87 # Write the <cp:coreProperties> element. 

88 

89 xmlns_cp = ( 

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

91 + "metadata/core-properties" 

92 ) 

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

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

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

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

97 

98 attributes = [ 

99 ("xmlns:cp", xmlns_cp), 

100 ("xmlns:dc", xmlns_dc), 

101 ("xmlns:dcterms", xmlns_dcterms), 

102 ("xmlns:dcmitype", xmlns_dcmitype), 

103 ("xmlns:xsi", xmlns_xsi), 

104 ] 

105 

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

107 

108 def _write_dc_creator(self): 

109 # Write the <dc:creator> element. 

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

111 

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

113 

114 def _write_cp_last_modified_by(self): 

115 # Write the <cp:lastModifiedBy> element. 

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

117 

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

119 

120 def _write_dcterms_created(self): 

121 # Write the <dcterms:created> element. 

122 attributes = [("xsi:type", "dcterms:W3CDTF")] 

123 self._xml_data_element("dcterms:created", self.iso_date, attributes) 

124 

125 def _write_dcterms_modified(self): 

126 # Write the <dcterms:modified> element. 

127 attributes = [("xsi:type", "dcterms:W3CDTF")] 

128 self._xml_data_element("dcterms:modified", self.iso_date, attributes) 

129 

130 def _write_dc_title(self): 

131 # Write the <dc:title> element. 

132 if "title" in self.properties: 

133 data = self.properties["title"] 

134 else: 

135 return 

136 

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

138 

139 def _write_dc_subject(self): 

140 # Write the <dc:subject> element. 

141 if "subject" in self.properties: 

142 data = self.properties["subject"] 

143 else: 

144 return 

145 

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

147 

148 def _write_cp_keywords(self): 

149 # Write the <cp:keywords> element. 

150 if "keywords" in self.properties: 

151 data = self.properties["keywords"] 

152 else: 

153 return 

154 

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

156 

157 def _write_dc_description(self): 

158 # Write the <dc:description> element. 

159 if "comments" in self.properties: 

160 data = self.properties["comments"] 

161 else: 

162 return 

163 

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

165 

166 def _write_cp_category(self): 

167 # Write the <cp:category> element. 

168 if "category" in self.properties: 

169 data = self.properties["category"] 

170 else: 

171 return 

172 

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

174 

175 def _write_cp_content_status(self): 

176 # Write the <cp:contentStatus> element. 

177 if "status" in self.properties: 

178 data = self.properties["status"] 

179 else: 

180 return 

181 

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