diff --git a/shortlister/model.py b/shortlister/model.py index 15cd19a..e72c17d 100644 --- a/shortlister/model.py +++ b/shortlister/model.py @@ -7,6 +7,8 @@ from openpyxl.utils import get_column_letter from openpyxl.styles import PatternFill, Font, Alignment from openpyxl.worksheet.table import Table,TableStyleInfo +from openpyxl.formatting.rule import Rule +from openpyxl.styles.differential import DifferentialStyle import pymupdf import re @@ -76,22 +78,10 @@ class Shortlist: } RANK_COLOUR_EXCEL = { - "U":{ - "start_color":"FF3300", - "fill_type":"solid" - }, - "M":{ - "start_color":"FFFF00", - "fill_type":"solid" - }, - "S":{ - "start_color":"C4D79B", - "fill_type":"solid" - }, - "E":{ - "start_color":"92D050", - "fill_type":"solid" - }, + "U":DifferentialStyle(font=Font(color="9C0006"), fill=PatternFill(bgColor="FFC7CE",fill_type="solid")), + "M": DifferentialStyle(font=Font(color="000000"),fill=PatternFill(bgColor="FFFF00", fill_type="solid")), + "S": DifferentialStyle(font=Font(color="000000"),fill=PatternFill(bgColor="C4D79B", fill_type="solid")), + "E": DifferentialStyle(font=Font(color="000000"),fill=PatternFill(bgColor="92D050", fill_type="solid")), } # Functions @@ -403,9 +393,11 @@ def export_excel(filename, applicants: List[Applicant], criteria: List[Criterion ws.append(flat_list) # Styling + # Auto adjust width for name column max_length = 0 - for cell in ws["B"]: + name_column = [ws[f"B{i}"] for i in range(1, ws.max_row + 1)] + for cell in name_column: try: if len(str(cell.value)) > max_length: max_length = len(cell.value) @@ -414,21 +406,14 @@ def export_excel(filename, applicants: List[Applicant], criteria: List[Criterion adjusted_width = (max_length + 1.5) * 1.2 ws.column_dimensions["B"].width = adjusted_width - # table styling - table_range = f"A1:{get_column_letter(ws.max_column)}{ws.max_row}" - - table = Table(displayName="DynamicTable", ref=table_range) - table.tableStyleInfo = TableStyleInfo(name="TableStyleMedium9", - showFirstColumn=False, - showLastColumn=False, - showRowStripes=True, - showColumnStripes=False,) - ws.add_table(table) # change colour/style of headings for col in range(1, ws.max_column+1): - heading_cell = ws[get_column_letter(col) + "1"] - heading_cell.alignment = Alignment(horizontal="center") + heading_cell = ws[get_column_letter(col) + "1"] + if heading_cell.column_letter == "B": + heading_cell.alignment = Alignment(horizontal="left") + else: + heading_cell.alignment = Alignment(horizontal="center") heading_cell.font = Font(bold=True) heading_cell.fill = PatternFill( start_color="8DB4E2", fill_type="solid" @@ -439,18 +424,40 @@ def export_excel(filename, applicants: List[Applicant], criteria: List[Criterion heading_score_cell = ws[get_column_letter(col)+"1"] heading_score_cell.alignment = Alignment(horizontal="center",textRotation=90) - # add colour for cells depending on the score: U(red),M(yellow),S,E(green) + # alignment for row in ws.iter_rows(2): for cell in row: - cell.alignment = Alignment(horizontal="center") - if str(cell.value) in RANK_COLOUR_EXCEL: - colour_parameter = RANK_COLOUR_EXCEL.get(cell.value) - cell.fill = PatternFill(**colour_parameter) + if cell.column_letter == "B": + cell.alignment = Alignment(horizontal="left") else: - pass + cell.alignment = Alignment(horizontal="center") - wb.save(filename) + # table styling + table_range = f"A1:{get_column_letter(ws.max_column)}{ws.max_row}" + table = Table(displayName="DynamicTable", ref=table_range) + table.tableStyleInfo = TableStyleInfo(name="TableStyleMedium9", + showFirstColumn=False, + showLastColumn=False, + showRowStripes=True, + showColumnStripes=False,) + ws.add_table(table) + + # add colour for cells depending on the score: U(red),M(yellow),S,E(green) + + for score in RANK_COLOUR_EXCEL: + ws.conditional_formatting.add( + range_string=table_range, + cfRule=Rule( + type="containsText", + operator="containsText", + text=score, + dxf=RANK_COLOUR_EXCEL[score], + formula=[f'EXACT("{score}",A1)'] + ) + ) + + wb.save(filename) def abbreviate(list_of_strings: List[str]) -> list[str]: """Create abbreviations for all strings in a list."""