@@ -95,10 +95,8 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
95
95
# Make column headers
96
96
column_titles = column_header .get ("titles" , [])
97
97
98
- # If we have results, then flatten field
99
- # names
98
+ # If we have results, then flatten field names
100
99
if len (results ):
101
-
102
100
# Set `xlsx_use_labels = True` inside the API View to enable labels.
103
101
use_labels = getattr (drf_view , "xlsx_use_labels" , False )
104
102
@@ -109,13 +107,23 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
109
107
# I.e.: xlsx_boolean_labels: {True: "Yes", False: "No"}
110
108
self .boolean_display = getattr (drf_view , "xlsx_boolean_labels" , None )
111
109
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.:
113
112
# column_data_styles = {
114
113
# 'distance': {
115
114
# '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
+ # },
117
120
# '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
+ # },
119
127
# 'format': '0.00E+00'
120
128
# },
121
129
# }
@@ -128,7 +136,12 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
128
136
# show values of a dict in individual cols. Takes key, an optional label
129
137
# and value than can be callable
130
138
# Example:
131
- # {"Additional Col": { label: "Something (optional)", formatter: my_function }}
139
+ # {
140
+ # "Additional Col": {
141
+ # label: "Something (optional)",
142
+ # formatter: my_function
143
+ # }
144
+ # }
132
145
self .custom_cols = getattr (drf_view , "xlsx_custom_cols" , dict ())
133
146
134
147
# 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):
219
232
with TemporaryFile () as tmp :
220
233
save_workbook (wb , tmp )
221
234
tmp .seek (0 )
222
- virtual_workbook = tmp .read ()
223
-
235
+ virtual_workbook = tmp .read ()
224
236
225
237
return virtual_workbook
226
238
@@ -320,6 +332,7 @@ def _make_body(self, body, row, row_count):
320
332
column_count = 0
321
333
row_count += 1
322
334
flattened_row = self ._flatten_data (row )
335
+
323
336
for header_key in self .combined_header_dict :
324
337
if header_key == "row_color" :
325
338
continue
@@ -328,24 +341,29 @@ def _make_body(self, body, row, row_count):
328
341
field .cell (self .ws , row_count , column_count ) if field else self .ws .cell (
329
342
row_count , column_count
330
343
)
344
+
331
345
self .ws .row_dimensions [row_count ].height = body .get ("height" , 40 )
346
+
332
347
if "row_color" in row :
333
348
last_letter = get_column_letter (column_count )
334
349
cell_range = self .ws [
335
350
f"A{ row_count } " : f"{ last_letter } { row_count } "
336
351
]
337
352
fill = PatternFill (fill_type = "solid" , start_color = row ["row_color" ])
353
+
338
354
for r in cell_range :
339
355
for c in r :
340
356
c .fill = fill
341
357
342
358
def _drf_to_xlsx_field (self , key , value ) -> XLSXField :
343
359
field = self .fields_dict .get (key )
360
+
344
361
cell_style = (
345
362
XLSXStyle (self .column_data_styles .get (key ))
346
363
if key in self .column_data_styles
347
364
else None
348
365
)
366
+
349
367
kwargs = {
350
368
"key" : key ,
351
369
"value" : value ,
@@ -356,17 +374,18 @@ def _drf_to_xlsx_field(self, key, value) -> XLSXField:
356
374
or self .custom_mappings .get (key ),
357
375
"cell_style" : cell_style ,
358
376
}
377
+
359
378
if isinstance (field , BooleanField ):
360
379
return XLSXBooleanField (boolean_display = self .boolean_display , ** kwargs )
361
380
elif isinstance (field , (IntegerField , FloatField , DecimalField )):
362
381
return XLSXNumberField (** kwargs )
363
382
elif isinstance (field , (DateTimeField , DateField , TimeField )):
364
- return XLSXDateField (** kwargs )
365
-
383
+ return XLSXDateField (** kwargs )
366
384
elif (
367
385
isinstance (field , ListField )
368
386
or isinstance (value , Iterable )
369
387
and not isinstance (value , str )
370
388
):
371
389
return XLSXListField (list_sep = self .list_sep , ** kwargs )
390
+
372
391
return XLSXField (** kwargs )
0 commit comments