markup: add spreadsheet writing

This commit is contained in:
2019-10-18 16:40:15 -07:00
parent 2b498fd2e3
commit 4cde2233cd
3 changed files with 46 additions and 1 deletions

40
markup/spreadsheet.py Normal file
View File

@ -0,0 +1,40 @@
import os
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
# 'SS20' -> 'S20'
return s[:1] + s[2:]
def write_spreadsheet(matches, subdir, catname, filename):
if not matches:
print('write_spreadsheet: no matches. skipping.')
return
wb = Workbook()
ws = wb.active
ws.append(['Season', 'Gender', 'Name', 'Style Number', 'Color'])
for m in matches:
ws.append([format_season(m['season']), m['gender'], m['name'], m['material'], m['color']])
# # Python types will automatically be converted
# import datetime
# ws['A2'] = datetime.datetime.now()
# ws['A2'].style = 'Good'
# save
dir = os.path.join(WORKDIR, subdir, catname)
ensure_dir(dir)
path = os.path.join(dir, f"{filename}.xlsx")
wb.save(path)
set_file_perms(path)

View File

@ -20,6 +20,7 @@ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'procat2.settings')
django.setup()
from markup.matching import find_marked_products
from markup.spreadsheet import write_spreadsheet
def main(argv):
@ -43,7 +44,8 @@ def main(argv):
catname = re.sub(r'[^\w]', '_', catname)
matches = find_marked_products(fname, subdir, catname, debug=0)
print(matches)
print(f'{len(matches)} product matches')
write_spreadsheet(matches, subdir, catname, path.stem)
if __name__ == '__main__': sys.exit(main(sys.argv))