Files
Aislo/M02_MasterTemplete/M02_Hopyo_Excel.py
T
eomsangdonandClaude Opus 5.5 78a2d5db85 feat(857-1): 불러오기 책 한 번 · 종류 한 말 — 일위대가 시트 없는 엑셀도 올라감 · 재료판 무효 지문 · 판 셈 M01 한 벌
- 새 M02_San_Workbook — 올린 엑셀을 한 번만 엶(시트 목록 합집합 · xls 서식 책 · 종류 가름 한 말 kind_of)
- Hopyo_Excel.read_book · Hopyo_Grid.read · San_Book.sheets · Sangeun_FromHopyo.keep 이 같은 책을 받음 · 전역 _OPENED · _open · opened · _sheets 걷음
- 종류 가름 한 말 — Hopyo_Stmate.is_stmate · San_Book.kind_of 가 Workbook.kind_of 를 씀(단가산출근거 = STmate · 단가산출 = EST)
- 올림 길(Router._upload) = 단가산출 시트가 있으면 일위대가 시트 · 호표 없음도 받음(호표 빈 목록 · 단산 목록 보임) · B09 내역서 길(read_book 기본)은 그대로 거절
- 「고를 재료」 판 = 재료판지문(M01 지문 · 별칭 · 자재 칸 포함)이 다르면 다시 셈 · 판 셈 동안 M01 한 벌(M01_MasterData_Store_Cache.steady · M01 파일 안 고침)
- 수치: 올림 때 책 열기 2 → 1 · 거창 xlsx 올림 18.0 → 9.0 초 · 판 첫 셈 거창 21.9 → 4.2 · 북부청임도 31.3 → 1.0 · 봉화 34.9 → 1.6 초(재료 종 수 같음)
- 지킴: 857-0 블록 전수 3,241 다름 0 · 올림 지문 = 뜻한 바뀜만(2016사방 · 2026수목 거절 → 올라감 · 그 갈래만 다시 뜸 · 재료표 같음)
- 시험: 새 test_857_1_workbook(책 한 번 · 종류 · 빈 호표 올림 · 판 무효 · 자국 횟수) · test_857_import_golden 「--write 올림」 갈래 하나 다시 뜨기
- 낡은 시험 고침: test_m02_hopyo_excel(일위대가 · 단가산출 둘 다 없는 책 = 400 · 단가산출만 = 200 호표 0) · test_m02_hopyo_stmate · test_741(옛 _sheets · _xlsx 부름 → 책 한 벌)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 19:43:19 +09:00

328 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""M02 일위대가 실무자료 읽기(PLAN 324-1) — 설계내역서 엑셀 「일위대가」 시트 →
호표 · 줄 · 갈래 · 검산.
읽기 = 책 한 벌 `M02_San_Workbook.read`(857-1 · .xls xlrd · .xlsx openpyxl · 빈 칸 None) ·
시트 · 열은 머리 글(빈칸 뺌)로 찾음 — 호표 · 품명 · 규격 · 수량 · 단위 · 합계 · 노무비 ·
재료비 · 경비 · 비고(돈 머리 = 단가 열 · 다음 열 = 금액).
호표 줄 = 「제N호표」 · 호표 1 단위 값 = 금액 열 · 구성 줄 = 호표 칸 빔 · 품명 있음 ·
끝 = 다음 호표.
갈래 = % · 호표(비고 일위N참조) · 단가산출(단산N참조) · 기계(단위 hr) · 별도계상(값 없음) ·
자재(재료비만) · 노임(단위 인 + 우리 인력 이름) · 기타 — 규격은 원문 그대로 둠.
이음(338) = 비고 「단산 · 산근 · 단가산출 N(참조 · 호표)」 → 단산 N · 「일위 · 대가 · 일위대가 N」 →
호표 N(빈칸 무시) · 비고 빈 줄(노무비 · 경비 단가 있음)은 품명 + 단위가 같은 블록이 하나뿐이면
이음(줄 `이음` = 「이름」) · 하이퍼링크 안 읽음.
STmate 꼴(「일위대가표」 · 「단가산출근거」 시트) = `M02_Hopyo_Stmate.read` ·
문서 보안(DRM) 파일 = 400 글.
검산 = 줄 금액 floor(단가 × 수량 × 10) / 10 · 합계 단가 = 셋 합 ·
% 줄 = floor(바탕 × 율 / 100 × 10) / 10 · 호표 비목 = floor(줄 금액 합) · 총괄표 합계 —
어긋남은 글 목록(막지 않음).
행(329-1) = 호표 [제N호표 줄, 마지막 구성 줄] · 구성 줄 한 줄(엑셀 1부터) ·
근거(330-1) = 단산N참조 줄에 「단가산출」 블록 `{출처, 직종, 행}`(`M02_Hopyo_ReadEst.read`) ·
`단산` = 블록마다 `{번, 이름, 단위, 행, 출처, 직종, 글, 금액, 항목}`(338 · 글 = 머리 뺀 몸 줄) ·
`꼴` = 「엑셀꼴」 · 두 형식 끝에 `UnitLine.settle`(단위 줄 하위 · 재현 · 덮음 · 일위대가 줄
투영 · 778-3).
"""
from __future__ import annotations
import re
from decimal import ROUND_FLOOR, Decimal, InvalidOperation
from typing import Any
from M02_MasterTemplete import M02_Hopyo_Clause as hopyo_clause # 출처 풀이(781-1 · 785-1)
from M02_MasterTemplete import M02_Hopyo_ReadEst as hopyo_est # EST 단가산출 어댑터(776-2)
from M02_MasterTemplete import M02_Hopyo_Stmate as hopyo_stmate
from M02_MasterTemplete import M02_Hopyo_UnitLine as unit_line
from M02_MasterTemplete import M02_San_Workbook as workbook # 책 한 벌(857-1)
SHEET = "일위대가"
SUMMARY = "일위대가총괄표"
LISTS = ("단가산출총괄표", "일위대가목록표") # 엑셀 보기 목록 시트(779-1 격자) — 같이 읽음
COSTS = ("노무비", "재료비", "경비")
HEADS = ("호표", "품명", "규격", "수량", "단위", "비고")
NO_SHEET = "일위대가 시트 없음"
_HOPYO = re.compile(r"제\s*(\d+)\s*호표")
_REF = re.compile(r"^(?:(단산|산근|단가산출)|(일위대가|일위|대가))(\d+)(?:참조|호표)?$")
DRM = workbook.DRM
_MACHINE_UNITS = ("hr", "시간", "h")
_CODE_KIND = {
"D": "단가산출",
"B": "호표",
"L": "노임",
"X": "기계",
"S": "별도계상",
"W": "별도계상",
}
ExcelError = workbook.BookError # 엉터리 파일 · 시트 없음 — 길이 400 으로 답함
def _flat(value: Any) -> str:
return re.sub(r"\s+", "", str(value or ""))
def _text(value: Any) -> str:
if isinstance(value, float) and value == int(value):
value = int(value)
return str(value if value is not None else "").strip()
def _num(value: Any) -> Decimal | None:
if value is None or isinstance(value, bool) or value == "":
return None
try:
got = Decimal(str(value).replace(",", "").strip())
except InvalidOperation:
return None
return got if got.is_finite() else None
def _floor(value: Decimal, places: int = 1) -> Decimal:
step = Decimal(1).scaleb(-places)
return value.quantize(step, rounding=ROUND_FLOOR)
def _out(value: Decimal | None) -> int | float | None:
if value is None:
return None
return int(value) if value == value.to_integral_value() else float(value)
def _columns(rows: list[list[Any]], money: tuple[str, ...]) -> tuple[int, dict[str, int]]:
"""(자료 첫 줄, {머리: 열}) — 머리 글은 앞 10 줄 안 「호표」 · 「품명」 이 든 줄."""
for at, row in enumerate(rows[:10]):
flat = [_flat(v) for v in row]
if "호표" not in flat or "품명" not in flat:
continue
cols = {head: flat.index(head) for head in (*HEADS, *money) if head in flat}
below = [_flat(v) for v in rows[at + 1]] if at + 1 < len(rows) else []
start = at + 2 if "단가" in below or "금액" in below else at + 1
return start, cols
raise ExcelError(f"{SHEET} 시트 머리 글(호표 · 품명)을 못 찾음")
def _cell(row: list[Any], col: int | None) -> Any:
return row[col] if col is not None and col < len(row) else None
def _money(row: list[Any], cols: dict[str, int], offset: int) -> dict[str, Decimal | None]:
"""비목 넷(합계 = 계) — `offset` 0 = 단가 열 · 1 = 금액 열."""
return {
("계" if head == "합계" else head): _num(_cell(row, None if c is None else c + offset))
for head in ("합계", *COSTS)
for c in [cols.get(head)]
}
# ── 갈래 · 검산 ─────────────────────────────────────────────────────────
def kind_of(line: dict[str, Any]) -> str:
"""구성 줄 갈래 한 규칙(776-2 · EST · STmate · 붙여넣기) — 단가산출 · 자재 · 기계 · 노임 ·
호표 · % · 별도계상 · 기타. STmate 코드가 있으면 코드 머리 글자 · 없으면 칸(비고 참조 ·
단위 · 단가) · 끝에 노임 올림(단위 「인」 + 우리 직종 · 괄호 글 풂)."""
kind = _code_kind(line) if line.get("코드") else _cell_kind(line)
if kind in ("기타", "자재", "노임") and _flat(line.get("단위")) == "인":
return "노임" if hopyo_est.trade(line.get("이름")) else kind
return kind
def _code_kind(line: dict[str, Any]) -> str:
code, price = str(line["코드"]), line["단가"]
if code.startswith("PR") or _flat(line.get("단위")) == "%":
return "%"
if code[0] == "M":
apart = str(line.get("비고") or "").startswith("별산")
apart = apart or "별도계상" in f"{line.get('규격')}{line.get('이름')}"
return "별도계상" if apart or not any(price.values()) else "자재"
if code[0] == "D" and price.get("재료비") and not price.get("노무비") and not price.get("경비"):
return "자재" # 재료비만 = 자재(358-2 · 엑셀꼴과 같음)
return _CODE_KIND.get(code[0], "기타")
def _cell_kind(line: dict[str, Any]) -> str:
ref = line.get("참조") or {}
unit = _flat(line.get("단위")).lower()
price = line["단가"]
if unit == "%":
return "%"
if ref.get("종류") == "일위":
return "호표"
if price.get("재료비") and not price.get("노무비") and not price.get("경비"):
return "자재" # 재료비만 = 자재(「단산N참조」여도 · 358-2)
if ref.get("종류") == "단산":
return "단가산출"
if unit in _MACHINE_UNITS:
return "기계"
if unit == "인" and _flat(line.get("이름")) in hopyo_est.trade_names():
return "노임"
if not any(price.get(k) for k in ("계", *COSTS)):
return "별도계상"
return "기타"
def _check_line(where: str, line: dict[str, Any]) -> list[str]:
qty, price, amount = line["수량"], line["단가"], line["금액"]
bad = []
if line["갈래"] == "%":
base = price.get("계")
want = _floor(base * qty / 100) if base is not None and qty else None
got = sum((amount.get(k) or Decimal(0)) for k in COSTS)
if want is not None and got != want:
bad.append(f"{where} · % 금액 {got} ≠ {want}")
return bad
if qty is None:
return bad
for k in COSTS:
if price.get(k) is None:
continue
want = _floor(price[k] * qty)
if (amount.get(k) or Decimal(0)) != want:
bad.append(f"{where} · {k} 금액 {amount.get(k)} ≠ {want}")
parts = [price.get(k) for k in COSTS]
if price.get("계") is not None and any(p is not None for p in parts):
total = sum((p or Decimal(0)) for p in parts)
if total != price["계"]:
bad.append(f"{where} · 합계 단가 {price['계']} ≠ {total}")
return bad
def _check_hopyo(hopyo: dict[str, Any], lines: list[dict[str, Any]]) -> list[str]:
where = f"제{hopyo['번']}호표"
bad = []
for line in lines:
bad += _check_line(f"{where} {line['차례'] + 1}째 줄 「{line['이름']}」", line)
if not lines:
return bad
for k in COSTS:
total = sum((line["금액"].get(k) or Decimal(0)) for line in lines)
want, got = _floor(total, 0), hopyo["금액"].get(k) or Decimal(0)
if got != want:
bad.append(f"{where} · {k} {got} ≠ 줄 합 {want}")
return bad
def _summary(rows: list[list[Any]] | None) -> dict[int, Decimal]:
"""총괄표 {호표 번: 합계} — 머리 글이 없으면 빈 것."""
if not rows:
return {}
try:
start, cols = _columns(rows, ("합계",))
except ExcelError:
return {}
out = {}
for row in rows[start:]:
hit = _HOPYO.fullmatch(_flat(_cell(row, cols.get("호표"))))
total = _num(_cell(row, cols.get("합계")))
if hit and total is not None:
out[int(hit.group(1))] = total
return out
def _by_name(raw: list[list[dict[str, Any]]], blocks: dict[int, dict[str, Any]]) -> None:
"""비고 빈 줄(노무비 · 경비 단가 있음) → 품명 + 단위가 같은 블록이 하나뿐이면 단산 이음."""
index: dict[tuple[str, str], list[int]] = {}
for no, block in blocks.items():
index.setdefault((_flat(block["이름"]), _flat(block["단위"]).lower()), []).append(no)
for line in (one for lines in raw for one in lines):
if line["참조"] or line["비고"] or line["갈래"] == "%":
continue
if not (line["단가"].get("노무비") or line["단가"].get("경비")):
continue
hit = index.get((_flat(line["이름"]), _flat(line["단위"]).lower())) or []
if len(hit) == 1:
line["참조"], line["이음"] = {"종류": "단산", "번": hit[0]}, "이름"
line["갈래"] = kind_of(line)
# ── 읽기 ──────────────────────────────────────────────────────────────
def read_book(
name: str, data: bytes, book: workbook.Book | None = None, blocks_only: bool = False
) -> dict[str, Any]:
"""엑셀 → `{시트, 호표: [...], 검산: {어긋남: [글 …], 총괄표: 맞춘 수}}` — 값은 JSON 수 ·
`book` = 올림 길이 한 번 연 책(857-1) · `blocks_only`(올림 길) = 단가산출 시트가 있으면 일위대가
시트 · 호표 없음도 받음(호표 빈 목록 · 단산만)."""
book = book or workbook.read(name, data)
sheets, loose = book.sheets, blocks_only and book.kind is not None
if book.kind == "STmate":
return unit_line.settle(hopyo_clause.settle(hopyo_stmate.read(sheets, loose)))
rows = sheets.get(SHEET)
if rows is None and not loose:
raise ExcelError(NO_SHEET)
start, cols = _columns(rows, ("합계", *COSTS)) if rows is not None else (0, {})
hopyos: list[dict[str, Any]] = []
raw: list[list[dict[str, Any]]] = []
for at, row in enumerate((rows or [])[start:], start + 1):
head = _flat(_cell(row, cols.get("호표")))
name_ = _text(_cell(row, cols.get("품명")))
hit = _HOPYO.fullmatch(head)
if hit:
hopyos.append(
{
"번": int(hit.group(1)),
"이름": name_,
"규격": _text(_cell(row, cols.get("규격"))),
"단위": _text(_cell(row, cols.get("단위"))),
"금액": _money(row, cols, 1),
"행": [at, at],
}
)
raw.append([])
continue
if head or not name_ or not hopyos:
continue
note = _text(_cell(row, cols.get("비고")))
ref = _REF.match(_flat(note))
line = {
"차례": len(raw[-1]),
"이름": name_,
"규격": _text(_cell(row, cols.get("규격"))),
"수량": _num(_cell(row, cols.get("수량"))),
"단위": _text(_cell(row, cols.get("단위"))),
"단가": _money(row, cols, 0),
"금액": _money(row, cols, 1),
"비고": note,
"참조": {"종류": "단산" if ref.group(1) else "일위", "번": int(ref.group(3))}
if ref
else None,
}
line["갈래"] = kind_of(line)
line["행"] = hopyos[-1]["행"][1] = at
raw[-1].append(line)
if not hopyos and not loose:
raise ExcelError(f"{SHEET} 시트에 호표(제N호표)가 없음")
blocks = hopyo_est.read(sheets.get(hopyo_est.SHEET))
_by_name(raw, blocks)
for line in (one for lines in raw for one in lines):
ref = line["참조"] or {}
if ref.get("종류") == "단산" and (block := blocks.get(ref["번"])):
line["근거"] = {k: block[k] for k in ("출처", "직종", "행")}
wrong: list[str] = []
totals = _summary(sheets.get(SUMMARY))
for hopyo, lines in zip(hopyos, raw, strict=True):
wrong += _check_hopyo(hopyo, lines)
total = hopyo["금액"].get("계")
if hopyo["번"] in totals and total is not None and totals[hopyo["번"]] != total:
wrong.append(f"제{hopyo['번']}호표 · 총괄표 합계 {totals[hopyo['번']]} ≠ {total}")
out = [
{**_json(hopyo), "줄": [_json(line) for line in lines]}
for hopyo, lines in zip(hopyos, raw, strict=True)
]
keep = ("번", "이름", "단위", "행", "출처", "직종", "글", "글행", "금액", "항목", "절")
basis = [{k: b[k] for k in keep} for b in blocks.values()]
book = {
"꼴": "엑셀꼴",
"시트": SHEET,
"호표": out,
"단산": basis,
"검산": {"어긋남": wrong, "총괄표": len(totals)},
}
return unit_line.settle(
hopyo_clause.settle(book)
) # 출처 풀이(781-1) · 단위 줄 하위 · 재현 · 덮음 · 일위대가 줄 투영(778-3)
def _json(value: Any) -> Any:
if isinstance(value, Decimal):
return _out(value)
if isinstance(value, dict):
return {k: _json(v) for k, v in value.items()}
return value