@@ -95,10 +95,8 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
9595 # Make column headers
9696 column_titles = column_header .get ("titles" , [])
9797
98- # If we have results, then flatten field
99- # names
98+ # If we have results, then flatten field names
10099 if len (results ):
101-
102100 # Set `xlsx_use_labels = True` inside the API View to enable labels.
103101 use_labels = getattr (drf_view , "xlsx_use_labels" , False )
104102
@@ -109,13 +107,23 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
109107 # I.e.: xlsx_boolean_labels: {True: "Yes", False: "No"}
110108 self .boolean_display = getattr (drf_view , "xlsx_boolean_labels" , None )
111109
112- # Set dict named column_data_styles with headers as keys and style as value. i.e.
110+ # Set dict named column_data_styles with headers as keys and styles as
111+ # values, I.e.:
113112 # column_data_styles = {
114113 # 'distance': {
115114 # 'fill': {'fill_type': 'solid', 'start_color': 'FFCCFFCC'},
116- # 'alignment': {'horizontal': 'center', 'vertical': 'center', 'wrapText': True, 'shrink_to_fit': True},
115+ # 'alignment': {
116+ # 'horizontal': 'center',
117+ # 'vertical': 'center',
118+ # 'wrapText': True, 'shrink_to_fit': True
119+ # },
117120 # 'border_side': {'border_style': 'thin', 'color': 'FF000000'},
118- # 'font': {'name': 'Arial', 'size': 14, 'bold': True, 'color': 'FF000000'},
121+ # 'font': {
122+ # 'name': 'Arial',
123+ # 'size': 14,
124+ # 'bold': True,
125+ # 'color': 'FF000000'
126+ # },
119127 # 'format': '0.00E+00'
120128 # },
121129 # }
@@ -128,7 +136,12 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
128136 # show values of a dict in individual cols. Takes key, an optional label
129137 # and value than can be callable
130138 # Example:
131- # {"Additional Col": { label: "Something (optional)", formatter: my_function }}
139+ # {
140+ # "Additional Col": {
141+ # label: "Something (optional)",
142+ # formatter: my_function
143+ # }
144+ # }
132145 self .custom_cols = getattr (drf_view , "xlsx_custom_cols" , dict ())
133146
134147 # Map a specific key to a column (I.e. if the field returns a json) or pass
@@ -219,8 +232,7 @@ def _save_virtual_workbook(self, wb):
219232 with TemporaryFile () as tmp :
220233 save_workbook (wb , tmp )
221234 tmp .seek (0 )
222- virtual_workbook = tmp .read ()
223-
235+ virtual_workbook = tmp .read ()
224236
225237 return virtual_workbook
226238
@@ -320,6 +332,7 @@ def _make_body(self, body, row, row_count):
320332 column_count = 0
321333 row_count += 1
322334 flattened_row = self ._flatten_data (row )
335+
323336 for header_key in self .combined_header_dict :
324337 if header_key == "row_color" :
325338 continue
@@ -328,24 +341,29 @@ def _make_body(self, body, row, row_count):
328341 field .cell (self .ws , row_count , column_count ) if field else self .ws .cell (
329342 row_count , column_count
330343 )
344+
331345 self .ws .row_dimensions [row_count ].height = body .get ("height" , 40 )
346+
332347 if "row_color" in row :
333348 last_letter = get_column_letter (column_count )
334349 cell_range = self .ws [
335350 f"A{ row_count } " : f"{ last_letter } { row_count } "
336351 ]
337352 fill = PatternFill (fill_type = "solid" , start_color = row ["row_color" ])
353+
338354 for r in cell_range :
339355 for c in r :
340356 c .fill = fill
341357
342358 def _drf_to_xlsx_field (self , key , value ) -> XLSXField :
343359 field = self .fields_dict .get (key )
360+
344361 cell_style = (
345362 XLSXStyle (self .column_data_styles .get (key ))
346363 if key in self .column_data_styles
347364 else None
348365 )
366+
349367 kwargs = {
350368 "key" : key ,
351369 "value" : value ,
@@ -356,17 +374,18 @@ def _drf_to_xlsx_field(self, key, value) -> XLSXField:
356374 or self .custom_mappings .get (key ),
357375 "cell_style" : cell_style ,
358376 }
377+
359378 if isinstance (field , BooleanField ):
360379 return XLSXBooleanField (boolean_display = self .boolean_display , ** kwargs )
361380 elif isinstance (field , (IntegerField , FloatField , DecimalField )):
362381 return XLSXNumberField (** kwargs )
363382 elif isinstance (field , (DateTimeField , DateField , TimeField )):
364- return XLSXDateField (** kwargs )
365-
383+ return XLSXDateField (** kwargs )
366384 elif (
367385 isinstance (field , ListField )
368386 or isinstance (value , Iterable )
369387 and not isinstance (value , str )
370388 ):
371389 return XLSXListField (list_sep = self .list_sep , ** kwargs )
390+
372391 return XLSXField (** kwargs )
0 commit comments