Skip to content

Commit 2cfd6e5

Browse files
committed
fix(writer): Improve the styling of the excel exporter
1 parent 89e41ea commit 2cfd6e5

1 file changed

Lines changed: 62 additions & 17 deletions

File tree

dragonfly_trace/writer.py

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def rooms_to_trace700_matrix(rooms, si_units=False):
5555
'Acoustic Ceiling Resistance ({})'.format(r_unit),
5656
'Cooling Dry Bulb ({})'.format(temp_unit),
5757
'Heating Dry Bulb ({})'.format(temp_unit),
58-
'Relative Humidity (%)'
58+
'Relative Humidity (%)',
5959
'Cooling Driftpoint ({})'.format(temp_unit),
6060
'Heating Driftpoint ({})'.format(temp_unit),
6161
'Thermostat Cooling Schedule',
@@ -465,7 +465,7 @@ def model_to_trace700_workbook(
465465
contain all tables
466466
needed to specify room loads in TRACE 700.
467467
"""
468-
# check that we could successfully import
468+
# check that we could successfully import openpyxl
469469
assert openpyxl is not None, 'Export to Excel is only available in Python 3. ' \
470470
'Either switch to using Python 3 or use the model_to_trace700_csv instead.'
471471

@@ -480,26 +480,71 @@ def model_to_trace700_workbook(
480480
workbook = openpyxl.Workbook()
481481
# add the Room table
482482
ws = workbook.active
483-
ws.title = 'Rooms'
484-
title_cell = ws['A1']
485-
title_cell.value = 'Rooms'
486-
title_cell.font = openpyxl.styles.Font(size=16, bold=True)
487-
for row in room_matrix:
488-
ws.append(row)
483+
_add_workbook_table(ws, 'Rooms', room_matrix)
489484
# add the Airflows table
490485
ws = workbook.create_sheet('Airflows')
491-
ws['A1'] = 'Airflows'
492-
for row in airflows_matrix:
493-
ws.append(row)
486+
_add_workbook_table(ws, 'Airflows', airflows_matrix)
494487
# add the People & Lighting table
495488
ws = workbook.create_sheet('People & Lighting')
496-
ws['A1'] = 'People & Lighting'
497-
for row in people_and_lights_matrix:
498-
ws.append(row)
489+
_add_workbook_table(ws, 'People & Lighting', people_and_lights_matrix)
499490
# add the Miscellaneous Loads table
500491
ws = workbook.create_sheet('Miscellaneous Loads')
501-
ws['A1'] = 'Miscellaneous Loads'
502-
for row in misc_loads_matrix:
503-
ws.append(row)
492+
_add_workbook_table(ws, 'Miscellaneous Loads', misc_loads_matrix)
504493

505494
return workbook
495+
496+
497+
def _add_workbook_table(ws, title, matrix):
498+
# define formatting to be used throughout the excel
499+
title_font = openpyxl.styles.Font(size=16, bold=True)
500+
bold_font = openpyxl.styles.Font(bold=True)
501+
side = openpyxl.styles.Side(border_style='thin', color='000000')
502+
all_border = openpyxl.styles.Border(top=side, left=side, right=side, bottom=side)
503+
grey_fill = openpyxl.styles.PatternFill(
504+
start_color='D3D3D3', end_color='D3D3D3', fill_type='solid'
505+
)
506+
row_length = len(matrix[0])
507+
column_letter = openpyxl.utils.get_column_letter(row_length)
508+
509+
# add the title and create a border around the top row
510+
ws.title = title
511+
title_cell = ws['A1']
512+
title_cell.value = title
513+
title_cell.font = title_font
514+
for col_idx, cell in enumerate(ws['A1:{}1'.format(column_letter)][0]):
515+
border = cell.border
516+
left = side if col_idx == 0 else border.left
517+
right = side if col_idx == row_length - 1 else border.right
518+
cell.border = openpyxl.styles.Border(top=side, bottom=side, left=left, right=right)
519+
cell.fill = grey_fill
520+
521+
# add each row of the matrix to the sheet
522+
for row in matrix:
523+
ws.append(row)
524+
new_row_idx = ws.max_row
525+
for cell in ws[new_row_idx]:
526+
cell.border = all_border
527+
528+
# auto-fit the column width of the table to the text
529+
for col in ws.columns:
530+
max_length = 0
531+
column_letter = openpyxl.utils.get_column_letter(col[0].column)
532+
for cell in col:
533+
try:
534+
# measure length of the cell's string representation
535+
if len(str(cell.value)) > max_length:
536+
max_length = len(str(cell.value))
537+
except AttributeError:
538+
pass # not a cell that sets the max dimension
539+
# apply width
540+
adjusted_width = (max_length + 2)
541+
ws.column_dimensions[column_letter].width = adjusted_width
542+
543+
# put the first column and row in bold
544+
for cell in ws['A']:
545+
cell.font = bold_font
546+
cell.fill = grey_fill
547+
for cell in ws['2:2']:
548+
cell.font = bold_font
549+
cell.fill = grey_fill
550+
title_cell.font = title_font

0 commit comments

Comments
 (0)