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

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

72 statements  

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

2# 

3# Chartsheet - 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 

9from . import worksheet 

10from .drawing import Drawing 

11 

12 

13class Chartsheet(worksheet.Worksheet): 

14 """ 

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

33 

34 self.is_chartsheet = True 

35 self.drawing = None 

36 self.chart = None 

37 self.charts = [] 

38 self.zoom_scale_normal = 0 

39 self.orientation = 0 

40 self.protection = False 

41 

42 def set_chart(self, chart): 

43 """ 

44 Set the chart object for the chartsheet. 

45 Args: 

46 chart: Chart object. 

47 Returns: 

48 chart: A reference to the chart object. 

49 """ 

50 chart.embedded = False 

51 chart.protection = self.protection 

52 self.chart = chart 

53 self.charts.append([0, 0, chart, 0, 0, 1, 1]) 

54 return chart 

55 

56 def protect(self, password="", options=None): 

57 """ 

58 Set the password and protection options of the worksheet. 

59 

60 Args: 

61 password: An optional password string. 

62 options: A dictionary of worksheet objects to protect. 

63 

64 Returns: 

65 Nothing. 

66 

67 """ 

68 # This method is overridden from parent worksheet class. 

69 

70 # Chartsheets only allow a reduced set of protect options. 

71 copy = {} 

72 

73 if not options: 

74 options = {} 

75 

76 if options.get("objects") is None: 

77 copy["objects"] = False 

78 else: 

79 # Objects are default on for chartsheets, so reverse state. 

80 copy["objects"] = not options["objects"] 

81 

82 if options.get("content") is None: 

83 copy["content"] = True 

84 else: 

85 copy["content"] = options["content"] 

86 

87 copy["sheet"] = False 

88 copy["scenarios"] = True 

89 

90 # If objects and content are both off then the chartsheet isn't 

91 # protected, unless it has a password. 

92 if password == "" and copy["objects"] and not copy["content"]: 

93 return 

94 

95 if self.chart: 

96 self.chart.protection = True 

97 else: 

98 self.protection = True 

99 

100 # Call the parent method. 

101 super(Chartsheet, self).protect(password, copy) 

102 

103 ########################################################################### 

104 # 

105 # Private API. 

106 # 

107 ########################################################################### 

108 def _assemble_xml_file(self): 

109 # Assemble and write the XML file. 

110 

111 # Write the XML declaration. 

112 self._xml_declaration() 

113 

114 # Write the root worksheet element. 

115 self._write_chartsheet() 

116 

117 # Write the worksheet properties. 

118 self._write_sheet_pr() 

119 

120 # Write the sheet view properties. 

121 self._write_sheet_views() 

122 

123 # Write the sheetProtection element. 

124 self._write_sheet_protection() 

125 

126 # Write the printOptions element. 

127 self._write_print_options() 

128 

129 # Write the worksheet page_margins. 

130 self._write_page_margins() 

131 

132 # Write the worksheet page setup. 

133 self._write_page_setup() 

134 

135 # Write the headerFooter element. 

136 self._write_header_footer() 

137 

138 # Write the drawing element. 

139 self._write_drawings() 

140 

141 # Write the legacyDrawingHF element. 

142 self._write_legacy_drawing_hf() 

143 

144 # Close the worksheet tag. 

145 self._xml_end_tag("chartsheet") 

146 

147 # Close the file. 

148 self._xml_close() 

149 

150 def _prepare_chart(self, index, chart_id, drawing_id): 

151 # Set up chart/drawings. 

152 

153 self.chart.id = chart_id - 1 

154 

155 self.drawing = Drawing() 

156 self.drawing.orientation = self.orientation 

157 

158 self.external_drawing_links.append( 

159 ["/drawing", "../drawings/drawing" + str(drawing_id) + ".xml"] 

160 ) 

161 

162 self.drawing_links.append( 

163 ["/chart", "../charts/chart" + str(chart_id) + ".xml"] 

164 ) 

165 

166 ########################################################################### 

167 # 

168 # XML methods. 

169 # 

170 ########################################################################### 

171 

172 def _write_chartsheet(self): 

173 # Write the <worksheet> element. This is the root element. 

174 

175 schema = "http://schemas.openxmlformats.org/" 

176 xmlns = schema + "spreadsheetml/2006/main" 

177 xmlns_r = schema + "officeDocument/2006/relationships" 

178 

179 attributes = [("xmlns", xmlns), ("xmlns:r", xmlns_r)] 

180 

181 self._xml_start_tag("chartsheet", attributes) 

182 

183 def _write_sheet_pr(self): 

184 # Write the <sheetPr> element for Sheet level properties. 

185 attributes = [] 

186 

187 if self.filter_on: 

188 attributes.append(("filterMode", 1)) 

189 

190 if self.fit_page or self.tab_color: 

191 self._xml_start_tag("sheetPr", attributes) 

192 self._write_tab_color() 

193 self._write_page_set_up_pr() 

194 self._xml_end_tag("sheetPr") 

195 else: 

196 self._xml_empty_tag("sheetPr", attributes)