Feat: 배치결과 내보내기 시 선거참관인_배치_와꾸_ 템플릿에 채워넣기

This commit is contained in:
user01
2026-05-25 00:33:34 +09:00
parent 3c069b45e1
commit be4ff64137

81
app.py
View File

@@ -826,30 +826,63 @@ def export():
ws.append([w['name'], w['hope'], w['address'], w['dob'], w['phone']]) ws.append([w['name'], w['hope'], w['address'], w['dob'], w['phone']])
elif export_type == 'assigned': elif export_type == 'assigned':
ws = wb.active template_path = '선거참관인_배치_와꾸_.xlsx'
ws.title = '배치 현황' if os.path.exists(template_path):
ws.append(['이름', '연락처', '생년월일', '희망사항', '주소', '배치근무지', '근무지주소']) wb = openpyxl.load_workbook(template_path)
for w in t['workers']: wp_workers = {}
a_id = t['assignments'].get(w['id']) for w in t['workers']:
if not a_id: a_id = t['assignments'].get(w['id'])
continue if a_id and a_id in wp_lookup:
wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else '' wp_workers.setdefault(wp_lookup[a_id]['name'], []).append(w)
wa = wp_lookup[a_id]['address'] if a_id in wp_lookup else '' used_names = set()
ws.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], wn, wa]) for sn in wb.sheetnames:
ws2 = wb.create_sheet('근무지별 배치') ws = wb[sn]
ws2.append(['근무지명', '주소', '배치인원', '배치된근무자']) headers = [str(c.value or '').strip() for c in ws[1]]
addr_groups = {} name_col = find_header_idx(headers, ['성명', '이름'])
for wp in t['workplaces']: dob_col = find_header_idx(headers, ['생년월일', '생일'])
key = wp['address'] phone_col = find_header_idx(headers, ['연락처', '전화', '휴대폰'])
if key not in addr_groups: addr_col = find_header_idx(headers, ['주소', '위치', '소재지'])
addr_groups[key] = {'names': [], 'workplace_ids': []} time_col = find_header_idx(headers, ['시간대'])
addr_groups[key]['names'].append(wp['name']) wp_name_col = find_header_idx(headers, ['투표소'])
addr_groups[key]['workplace_ids'].append(wp['id']) if wp_name_col is None:
for addr, g in addr_groups.items(): continue
combined_name = ', '.join(g['names']) current_wp = ''
aw = [w['name'] for w in t['workers'] if t['assignments'].get(w['id']) in g['workplace_ids']] for cells in ws.iter_rows(min_row=2):
if aw: row = [c.value for c in cells]
ws2.append([combined_name, addr, len(aw), ', '.join(aw)]) v = str(row[wp_name_col] or '').strip()
if v:
current_wp = v
if not current_wp:
continue
time_slot = str(row[time_col] or '').strip() if time_col is not None else ''
candidates = [w for w in wp_workers.get(current_wp, [])
if w['id'] not in used_names
and (not time_slot or time_slot in w.get('hope', ''))]
if not candidates and time_slot:
candidates = [w for w in wp_workers.get(current_wp, [])
if w['id'] not in used_names]
for w in candidates[:1]:
if name_col is not None and cells[name_col].value is None:
cells[name_col].value = w['name']
if dob_col is not None and cells[dob_col].value is None:
cells[dob_col].value = w['dob']
if phone_col is not None and cells[phone_col].value is None:
cells[phone_col].value = w['phone']
if addr_col is not None and cells[addr_col].value is None:
cells[addr_col].value = w['address']
used_names.add(w['id'])
# 템플릿에 없는 나머지 배치된 근로자는 추가 시트로 내보내기
all_assigned = {w['id'] for w in t['workers'] if w['id'] in t['assignments']}
remaining = all_assigned - used_names
if remaining:
ws_extra = wb.create_sheet('추가 배치')
ws_extra.append(['이름', '연락처', '생년월일', '희망사항', '주소', '배치근무지', '근무지주소'])
for w in t['workers']:
if w['id'] in remaining:
a_id = t['assignments'].get(w['id'])
wn = wp_lookup[a_id]['name'] if a_id in wp_lookup else ''
wa = wp_lookup[a_id]['address'] if a_id in wp_lookup else ''
ws_extra.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], wn, wa])
else: # all (전체) else: # all (전체)
ws1 = wb.active ws1 = wb.active