markup/spreadsheet.py: add category + size, add styling

This commit is contained in:
2019-10-27 00:01:05 -07:00
parent 7e77a04239
commit 5c3ad32cb5

View File

@ -15,16 +15,40 @@ def format_season(s):
return s[:1] + s[2:] return s[:1] + s[2:]
def format_name(name, gender):
return '{}-{}'.format(name, gender[:1])
def write_spreadsheet(matches, workdir, file_base): def write_spreadsheet(matches, workdir, file_base):
if not matches: if not matches:
print('write_spreadsheet: no matches. skipping.') print('write_spreadsheet: no matches. skipping.')
return None return None
header_font = Font(name='Calibri', size=12, bold=True)
body_font = Font(name='Calibri', size=12)
header_fill = PatternFill(start_color="cccccc", end_color="cccccc", fill_type="solid")
body_fill = PatternFill(start_color="eeeeee", end_color="eeeeee", fill_type="solid")
thin_side = Side(border_style='thin', color='000000')
border = Border(bottom=thin_side)
wb = Workbook() wb = Workbook()
ws = wb.active ws = wb.active
# header row # header row
ws.append(['Season', 'Gender', 'Name', 'Style Number', 'Color']) ws.append(['Style Number', 'Product Name', 'Season', 'Color', 'Category', 'Size Range'])
# style the header row
ws.column_dimensions['A'].width = 15
ws.column_dimensions['B'].width = 30
ws.column_dimensions['C'].width = 10
ws.column_dimensions['D'].width = 30
ws.column_dimensions['E'].width = 15
ws.column_dimensions['F'].width = 35
for f in ('A1', 'B1', 'C1', 'D1', 'E1', 'F1'):
ws[f].font = header_font
ws[f].fill = header_fill
ws[f].border = border
# TODO: sort matches # TODO: sort matches
@ -33,17 +57,26 @@ def write_spreadsheet(matches, workdir, file_base):
for m in matches: for m in matches:
# in the case of kids, # in the case of kids,
# we might have multiple products in a match # we might have multiple products in a match
seasons = m['season'].split('\n') seasons = m['season'].split('\n')
genders = m['gender'].split('\n') genders = m['gender'].split('\n')
names = m['name'].split('\n') names = m['name'].split('\n')
materials = m['material'].split('\n') materials = m['material'].split('\n')
colors = m['color'].split('\n') colors = m['color'].split('\n')
sizes = m['size'].split('\n')
categories = m['category'].split('\n')
for s, g, n, m, c in zip_longest(seasons, genders, names, materials, colors, fillvalue=''): for s, g, n, m, c, sz, ct in zip_longest(seasons, genders, names, materials, colors, sizes, categories, fillvalue=''):
if not m in seen: if not m in seen:
ws.append([format_season(s), g, n, m, c]) ws.append([m, format_name(n, g), format_season(s), c, ct, sz])
seen[m] = True seen[m] = True
# style body
for row in ws.iter_rows(min_row=2, max_row=None, max_col=None):
for cell in row:
cell.font = body_font
cell.fill = body_fill
cell.border = border
# save # save
ensure_dir(workdir) ensure_dir(workdir)
path = os.path.join(workdir, f"{file_base}.xlsx") path = os.path.join(workdir, f"{file_base}.xlsx")