import openpyxl, json, os, re, sys
from pathlib import Path

base = Path('/Users/neo/.hermes/audits/cashflow-asset-hook-20260512-083049')
wb_path = base/'wanchuan_anhui_v9_v2_1_long_asset_R29_R58_asset_class_atomic_SAFE_20260513-085849.xlsx'
sheet = '长期资产循环_全量穿透校验'

wb_f = openpyxl.load_workbook(wb_path, data_only=False, read_only=False)
wb_v = openpyxl.load_workbook(wb_path, data_only=True, read_only=False)
ws_f = wb_f[sheet]
ws_v = wb_v[sheet]

cols = list(range(1, 18))  # A:Q
col_letters = [openpyxl.utils.get_column_letter(c) for c in cols]
print('WORKBOOK', wb_path)
print('SHEET', sheet, 'max_row', ws_f.max_row, 'max_col', ws_f.max_column)
print('COLS', col_letters)
print('\n=== TARGET ROWS 64:169 A:Q (non-empty cells; formula/value) ===')
for r in range(64, 170):
    row_items=[]
    label_parts=[]
    for c in cols:
        cell = ws_f.cell(r,c)
        val = cell.value
        v = ws_v.cell(r,c).value
        if val is not None or v is not None:
            coord = cell.coordinate
            if isinstance(val, str) and val.startswith('='):
                s = f'{coord}:F={val} | V={v}'
            else:
                s = f'{coord}:T={val} | V={v}'
            row_items.append(s)
            if c<=2 and (val is not None or v is not None):
                label_parts.append(str(val if val is not None else v))
    if row_items:
        print(f'ROW {r} LABEL={" / ".join(label_parts)}')
        for s in row_items:
            print('  '+s)

# Print sheet names likely relevant
print('\n=== RELEVANT SHEETS ===')
for name in wb_f.sheetnames:
    if any(k in name for k in ['固定资产','在建工程','无形资产','长期待摊','投资性房地产','使用权资产','资产减值','长期应付款','现金流量表底稿','试算表','附注','数据源_CF']):
        print(name)

# Dump dimensions/used labels for asset sheets relevant rows/cols
asset_sheets = ['固定资产','在建工程情况表','无形资产变动表','长期待摊费用变动明细表','投资性房地产','使用权资产 ','资产减值明细表','长期应付款明细','试算表','附注','现金流量表底稿','数据源_CF小合并']
for sh in asset_sheets:
    if sh not in wb_f.sheetnames:
        continue
    ws=wb_f[sh]; wsv=wb_v[sh]
    print(f'\n=== ASSET SHEET {sh} dimension {ws.max_row}x{ws.max_column} ===')
    max_r=min(ws.max_row, 90 if sh not in ['附注','现金流量表底稿','试算表','数据源_CF小合并'] else 260)
    max_c=min(ws.max_column, 18)
    for r in range(1, max_r+1):
        cells=[]
        # include row if first two cols text or formulas/values in key cols
        nonempty=False
        for c in range(1,max_c+1):
            val=ws.cell(r,c).value; vv=wsv.cell(r,c).value
            if val is not None or vv is not None:
                nonempty=True
                break
        if not nonempty: continue
        # restrict some large sheets to keyword or formula/value rows around known data
        if sh in ['附注']:
            vals=' '.join(str(ws.cell(r,c).value or '') for c in range(1, min(ws.max_column,12)+1))
            if not any(k in vals for k in ['固定资产','在建工程','无形资产','长期待摊','投资性房地产','使用权资产','开发支出','其他非流动资产','应付','预付','销项','进项','减值','报废','处置','利息资本化']) and r not in list(range(360,366))+list(range(680,695))+list(range(1025,1070))+list(range(1220,1250)):
                continue
        if sh in ['试算表'] and not (35<=r<=80 or 1<=r<=5 or 20<=r<=25):
            continue
        if sh in ['现金流量表底稿'] and not (180<=r<=205 or 238<=r<=251 or 405<=r<=425):
            continue
        if sh in ['数据源_CF小合并'] and not (35<=r<=75):
            continue
        for c in range(1,max_c+1):
            val=ws.cell(r,c).value; vv=wsv.cell(r,c).value
            if val is not None or vv is not None:
                coord=ws.cell(r,c).coordinate
                if isinstance(val,str) and val.startswith('='):
                    cells.append(f'{coord}={val}=>{vv}')
                else:
                    cells.append(f'{coord}={val if val is not None else vv}')
        if cells:
            print('ROW',r,' | '.join(cells))
