# 读取 Doc目录 数据库-2015-2024-v2.xlsx 文件内容 # pip install openpyxl import openpyxl from Config.Config import EXCEL_PATH from Util.AreaUtil import query_area_info file_name = EXCEL_PATH # 初始化统计变量 success_count = 0 error_count = 0 error_details = [] # 读取Excel文件并获取所有sheet名称 try: # 加载工作簿 workbook = openpyxl.load_workbook(file_name, read_only=True) sheet_names = workbook.sheetnames if sheet_names: first_sheet = workbook[sheet_names[0]] # 读取所有行数据 rows = list(first_sheet.iter_rows(values_only=True)) total_count = len(rows[2:]) # 排除表头的总数据行数 # 跳过前两行表头,从第三行开始处理数据 for row_num, row in enumerate(rows[2:], start=3): # 假设行政区划名称在第一列(索引0) if row and row[0]: area_name = row[0] result = query_area_info(area_name) if result: success_count += 1 print(f"✅ 行 {row_num}: {area_name} -> 全称: {result['full_name']}, 行政区划码: {result['area_code']}") else: error_count += 1 error_details.append(f"行 {row_num}: '{area_name}'") print(f"❌ 行 {row_num}: 未找到 '{area_name}' 的相关信息") # 输出汇总结果 print("\n==================== 匹配结果汇总 ====================") print(f"📊 总记录数: {total_count}, 成功匹配: {success_count}, 匹配失败: {error_count}") print(f"📈 匹配成功率: {success_count/total_count*100:.2f}%\n") if error_details: print("❌ 匹配失败详情:") for detail in error_details: print(f" - {detail}") else: print("✅ 所有记录均成功匹配!") workbook.close() except FileNotFoundError: print(f"🔴 错误:找不到文件 '{file_name}'") except Exception as e: print(f"🔴 读取Excel时发生错误:{str(e)}")