# -*- coding: utf-8 -*- """用 Excel COM 重算公式并扫描公式错误""" import win32com.client import os path = r"D:\Projects\phpstudy_pro\WWW\yjyf\doc\测试用例-全系统.xlsx" excel = win32com.client.DispatchEx("Excel.Application") excel.Visible = False excel.DisplayAlerts = False try: wb = excel.Workbooks.Open(path) excel.CalculateFullRebuild() errors = [] for ws in wb.Worksheets: used = ws.UsedRange for row in range(1, used.Rows.Count + 1): for col in range(1, used.Columns.Count + 1): v = ws.Cells(row, col).Text if v.startswith("#") and any(e in v for e in ("REF!", "DIV/0!", "VALUE!", "N/A", "NAME?")): errors.append(f"{ws.Name}!{ws.Cells(row, col).Address}={v}") print("formula errors:", len(errors)) for e in errors[:10]: print(" -", e) # 抽查统计 sheet 计算值 ws2 = wb.Worksheets("用例统计") last = ws2.Cells(ws2.UsedRange.Rows.Count, 1).Row print(f"用例统计合计行: 用例数={ws2.Cells(last,2).Value} 通过={ws2.Cells(last,3).Value} 失败={ws2.Cells(last,4).Value}") wb.Save() wb.Close(False) finally: excel.Quit() print("done")