From da3ff199d870aadde55cdc5e0a9c800fa6b6fb40 Mon Sep 17 00:00:00 2001 From: king Date: Wed, 27 May 2026 00:19:46 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=97=91=EC=85=80=20A~C?= =?UTF-8?q?=20=EC=97=B4=20=EB=84=88=EB=B9=84=20=EC=9E=90=EB=8F=99=20?= =?UTF-8?q?=EC=A1=B0=EC=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 콘텐츠 길이에 맞춰 열 너비를 계산해서 적용한다. 한글은 ASCII 폭의 약 2배로 가중치를 두고, 최소 10 · 최대 60 으로 클램프해 너무 좁거나 넓지 않게 한다. Co-Authored-By: Claude Sonnet 4.6 --- app/routers/orders.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/routers/orders.py b/app/routers/orders.py index d365d2e..89e54bf 100644 --- a/app/routers/orders.py +++ b/app/routers/orders.py @@ -816,9 +816,30 @@ def upload_file( df_out = pd.DataFrame(excel_rows, columns=["상품코드[필수]", "가용수량", "불용수량"]) # 엑셀 파일 쓰기 (1행 헤더, 2행부터 데이터 — pandas to_excel 기본 동작) + # 작성 직후 A~C 열 너비를 콘텐츠 길이에 맞춰 자동 조절한다. + # (한글은 ASCII보다 폭이 약 2배이므로 가중치 적용) + def _excel_display_len(value) -> int: + s = "" if value is None else str(value) + width = 0 + for ch in s: + width += 2 if ord(ch) > 127 else 1 + return width + excel_io = io.BytesIO() with pd.ExcelWriter(excel_io, engine='openpyxl') as writer: df_out.to_excel(writer, index=False, sheet_name="발주") + worksheet = writer.sheets["발주"] + from openpyxl.utils import get_column_letter + for col_idx, col_name in enumerate(df_out.columns, start=1): + header_w = _excel_display_len(col_name) + data_w = max( + (_excel_display_len(v) for v in df_out[col_name].values), + default=0, + ) + # 최소 10, 최대 60 으로 클램프 + 여백 2 + worksheet.column_dimensions[get_column_letter(col_idx)].width = ( + min(60, max(10, max(header_w, data_w) + 2)) + ) excel_io.seek(0) # 6. 다운로드 파일명: "MM월 DD일 (요일)_발주_(낱개 상품 출고).xls"