# -*- coding: utf-8 -*-
"""후행 빈/잔재 행 삭제: python _trim_tail.py {xlsx경로}"""
import sys,io,openpyxl
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
f=sys.argv[1]
wb=openpyxl.load_workbook(f); ws=wb.active
N=ws.max_row; last=2
for r in range(3,N+1):
    if ws.cell(r,2).value is not None: last=r
if N>last:
    for m in list(ws.merged_cells.ranges):
        if m.min_row>last: ws.unmerge_cells(str(m))
    ws.delete_rows(last+1, N-last)
    wb.save(f)
    print(f'{last-2}행 유지·후행 {N-last}행 삭제')
else:
    print(f'{last-2}행·후행잔재없음')
