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']])
elif export_type == 'assigned':
ws = wb.active
ws.title = '배치 현황'
ws.append(['이름', '연락처', '생년월일', '희망사항', '주소', '배치근무지', '근무지주소'])
for w in t['workers']:
a_id = t['assignments'].get(w['id'])
if not a_id:
continue
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.append([w['name'], w['phone'], w['dob'], w['hope'], w['address'], wn, wa])
ws2 = wb.create_sheet('근무지별 배치')
ws2.append(['근무지명', '주소', '배치인원', '배치된근무자'])
addr_groups = {}
for wp in t['workplaces']:
key = wp['address']
if key not in addr_groups:
addr_groups[key] = {'names': [], 'workplace_ids': []}
addr_groups[key]['names'].append(wp['name'])
addr_groups[key]['workplace_ids'].append(wp['id'])
for addr, g in addr_groups.items():
combined_name = ', '.join(g['names'])
aw = [w['name'] for w in t['workers'] if t['assignments'].get(w['id']) in g['workplace_ids']]
if aw:
ws2.append([combined_name, addr, len(aw), ', '.join(aw)])
template_path = '선거참관인_배치_와꾸_.xlsx'
if os.path.exists(template_path):
wb = openpyxl.load_workbook(template_path)
wp_workers = {}
for w in t['workers']:
a_id = t['assignments'].get(w['id'])
if a_id and a_id in wp_lookup:
wp_workers.setdefault(wp_lookup[a_id]['name'], []).append(w)
used_names = set()
for sn in wb.sheetnames:
ws = wb[sn]
headers = [str(c.value or '').strip() for c in ws[1]]
name_col = find_header_idx(headers, ['성명', '이름'])
dob_col = find_header_idx(headers, ['생년월일', '생일'])
phone_col = find_header_idx(headers, ['연락처', '전화', '휴대폰'])
addr_col = find_header_idx(headers, ['주소', '위치', '소재지'])
time_col = find_header_idx(headers, ['시간대'])
wp_name_col = find_header_idx(headers, ['투표소'])
if wp_name_col is None:
continue
current_wp = ''
for cells in ws.iter_rows(min_row=2):
row = [c.value for c in cells]
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 (전체)
ws1 = wb.active