54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
import os
|
|
from itertools import zip_longest
|
|
|
|
from openpyxl import Workbook
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
|
|
|
|
from .utils import ensure_dir, set_file_perms, WORKDIR
|
|
|
|
|
|
def format_season(s):
|
|
if not s or len(s) < 4:
|
|
return s
|
|
|
|
# 'FW20' -> 'F20'
|
|
return s[:1] + s[2:]
|
|
|
|
|
|
def write_spreadsheet(matches, workdir, file_base):
|
|
if not matches:
|
|
print('write_spreadsheet: no matches. skipping.')
|
|
return None
|
|
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
|
|
# header row
|
|
ws.append(['Season', 'Gender', 'Name', 'Style Number', 'Color'])
|
|
|
|
# TODO: sort matches
|
|
|
|
seen = {}
|
|
|
|
for m in matches:
|
|
# in the case of kids,
|
|
# we might have multiple products in a match
|
|
seasons = m['season'].split('\n')
|
|
genders = m['gender'].split('\n')
|
|
names = m['name'].split('\n')
|
|
materials = m['material'].split('\n')
|
|
colors = m['color'].split('\n')
|
|
|
|
for s, g, n, m, c in zip_longest(seasons, genders, names, materials, colors, fillvalue=''):
|
|
if not m in seen:
|
|
ws.append([format_season(s), g, n, m, c])
|
|
seen[m] = True
|
|
|
|
# save
|
|
ensure_dir(workdir)
|
|
path = os.path.join(workdir, f"{file_base}.xlsx")
|
|
wb.save(path)
|
|
set_file_perms(path)
|
|
|
|
return path
|