From be4ff641378b530f832af69ceff1c8eee14b8c25 Mon Sep 17 00:00:00 2001 From: user01 Date: Mon, 25 May 2026 00:33:34 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=B0=B0=EC=B9=98=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EB=82=B4=EB=B3=B4=EB=82=B4=EA=B8=B0=20=EC=8B=9C=20=EC=84=A0?= =?UTF-8?q?=EA=B1=B0=EC=B0=B8=EA=B4=80=EC=9D=B8=5F=EB=B0=B0=EC=B9=98=5F?= =?UTF-8?q?=EC=99=80=EA=BE=B8=5F=20=ED=85=9C=ED=94=8C=EB=A6=BF=EC=97=90=20?= =?UTF-8?q?=EC=B1=84=EC=9B=8C=EB=84=A3=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 81 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 2217d4c..5058652 100644 --- a/app.py +++ b/app.py @@ -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