Skip to content

Commit

Permalink
Merge pull request #83 from UCL-ARC/81-export-enhance
Browse files Browse the repository at this point in the history
81 export enhance
  • Loading branch information
shiyingwucl authored Jan 17, 2025
2 parents aecf353 + a202463 commit 606bbfe
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions shortlister/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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."""
Expand Down

0 comments on commit 606bbfe

Please sign in to comment.