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

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

50 statements  

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

2# 

3# Custom - A class for writing the Excel XLSX Custom Property 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 Custom(xmlwriter.XMLwriter): 

14 """ 

15 A class for writing the Excel XLSX Custom Workbook Property 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(Custom, self).__init__() 

33 

34 self.properties = [] 

35 self.pid = 1 

36 

37 def _set_properties(self, properties): 

38 # Set the document properties. 

39 self.properties = properties 

40 

41 ########################################################################### 

42 # 

43 # Private API. 

44 # 

45 ########################################################################### 

46 

47 def _assemble_xml_file(self): 

48 # Assemble and write the XML file. 

49 

50 # Write the XML declaration. 

51 self._xml_declaration() 

52 

53 self._write_properties() 

54 

55 self._xml_end_tag("Properties") 

56 

57 # Close the file. 

58 self._xml_close() 

59 

60 ########################################################################### 

61 # 

62 # XML methods. 

63 # 

64 ########################################################################### 

65 

66 def _write_properties(self): 

67 # Write the <Properties> element. 

68 schema = "http://schemas.openxmlformats.org/officeDocument/2006/" 

69 xmlns = schema + "custom-properties" 

70 xmlns_vt = schema + "docPropsVTypes" 

71 

72 attributes = [ 

73 ("xmlns", xmlns), 

74 ("xmlns:vt", xmlns_vt), 

75 ] 

76 

77 self._xml_start_tag("Properties", attributes) 

78 

79 for custom_property in self.properties: 

80 # Write the property element. 

81 self._write_property(custom_property) 

82 

83 def _write_property(self, custom_property): 

84 # Write the <property> element. 

85 

86 fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" 

87 

88 name, value, property_type = custom_property 

89 self.pid += 1 

90 

91 attributes = [ 

92 ("fmtid", fmtid), 

93 ("pid", self.pid), 

94 ("name", name), 

95 ] 

96 

97 self._xml_start_tag("property", attributes) 

98 

99 if property_type == "number_int": 

100 # Write the vt:i4 element. 

101 self._write_vt_i4(value) 

102 elif property_type == "number": 

103 # Write the vt:r8 element. 

104 self._write_vt_r8(value) 

105 elif property_type == "date": 

106 # Write the vt:filetime element. 

107 self._write_vt_filetime(value) 

108 elif property_type == "bool": 

109 # Write the vt:bool element. 

110 self._write_vt_bool(value) 

111 else: 

112 # Write the vt:lpwstr element. 

113 self._write_vt_lpwstr(value) 

114 

115 self._xml_end_tag("property") 

116 

117 def _write_vt_lpwstr(self, value): 

118 # Write the <vt:lpwstr> element. 

119 self._xml_data_element("vt:lpwstr", value) 

120 

121 def _write_vt_filetime(self, value): 

122 # Write the <vt:filetime> element. 

123 self._xml_data_element("vt:filetime", value) 

124 

125 def _write_vt_i4(self, value): 

126 # Write the <vt:i4> element. 

127 self._xml_data_element("vt:i4", value) 

128 

129 def _write_vt_r8(self, value): 

130 # Write the <vt:r8> element. 

131 self._xml_data_element("vt:r8", value) 

132 

133 def _write_vt_bool(self, value): 

134 # Write the <vt:bool> element. 

135 

136 if value: 

137 value = "true" 

138 else: 

139 value = "false" 

140 

141 self._xml_data_element("vt:bool", value)