2025-09-10 13:50:18 +08:00
|
|
|
import openpyxl
|
2025-09-10 11:15:03 +08:00
|
|
|
import json
|
|
|
|
import os
|
2025-09-10 13:50:18 +08:00
|
|
|
from typing import List, Dict, Any, Tuple
|
2025-09-10 11:15:03 +08:00
|
|
|
|
|
|
|
from Config.Config import EXCEL_PATH
|
2025-09-10 13:53:34 +08:00
|
|
|
from Util.DataUtil import (
|
|
|
|
init_directories, process_value, print_conversion_stats,
|
|
|
|
convert_area_name, save_to_json, load_workbook_sheet
|
|
|
|
)
|
2025-09-10 11:15:03 +08:00
|
|
|
|
2025-09-10 13:50:18 +08:00
|
|
|
# ======================= 配置常量 =======================
|
2025-09-10 11:15:03 +08:00
|
|
|
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Data')
|
2025-09-10 13:50:18 +08:00
|
|
|
JSON_PATH = os.path.join(DATA_DIR, 'MaoRuXueLv.json')
|
|
|
|
SHEET_NAME = '毛入学率'
|
|
|
|
START_ROW = 5
|
|
|
|
REGION_NAME_COLUMN = 'B'
|
|
|
|
YEAR_RANGE = [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024]
|
2025-09-10 11:15:03 +08:00
|
|
|
|
2025-09-10 13:50:18 +08:00
|
|
|
# 数据列映射配置
|
|
|
|
DATA_COLUMNS = {
|
|
|
|
# 学前教育 - 交替列映射
|
|
|
|
'preschool_enrollment': {
|
|
|
|
'columns': ['D', 'F', 'H', 'J', 'L', 'N', 'P', 'R', 'T', 'V'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
'preschool_enrollment_rate': {
|
|
|
|
'columns': ['E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
|
|
|
|
# 小学教育
|
|
|
|
'primary_enrollment': {
|
|
|
|
'columns': ['X', 'Z', 'AB', 'AD', 'AF', 'AH', 'AJ', 'AL', 'AN', 'AP'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
'primary_enrollment_rate': {
|
|
|
|
'columns': ['Y', 'AA', 'AC', 'AE', 'AG', 'AI', 'AK', 'AM', 'AO', 'AQ'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
|
|
|
|
# 初中教育
|
|
|
|
'junior_high_enrollment': {
|
|
|
|
'columns': ['AR', 'AT', 'AV', 'AX', 'AZ', 'BB', 'BD', 'BF', 'BH', 'BJ'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
'junior_high_enrollment_rate': {
|
|
|
|
'columns': ['AS', 'AU', 'AW', 'AY', 'BA', 'BC', 'BE', 'BG', 'BI', 'BK'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
|
|
|
|
# 普通高中教育
|
|
|
|
'senior_high_enrollment': {
|
|
|
|
'columns': ['BL', 'BN', 'BP', 'BR', 'BT', 'BV', 'BX', 'BZ', 'CB', 'CD'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
'senior_high_enrollment_rate': {
|
|
|
|
'columns': ['BM', 'BO', 'BQ', 'BS', 'BU', 'BW', 'BY', 'CA', 'CC', 'CE'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
|
|
|
|
# 中职教育
|
|
|
|
'vocational_enrollment': {
|
|
|
|
'columns': ['CF', 'CH', 'CJ', 'CL', 'CN', 'CP', 'CR', 'CT', 'CV', 'CX'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
},
|
|
|
|
'vocational_enrollment_rate': {
|
|
|
|
'columns': ['CG', 'CI', 'CK', 'CM', 'CO', 'CQ', 'CS', 'CU', 'CW', 'CY'],
|
|
|
|
'years': YEAR_RANGE
|
|
|
|
}
|
|
|
|
}
|
2025-09-10 11:15:03 +08:00
|
|
|
|
2025-09-10 13:50:18 +08:00
|
|
|
# ======================= 核心逻辑 =======================
|
|
|
|
def extract_enrollment_data(sheet: openpyxl.worksheet.worksheet.Worksheet) -> Tuple[List[Dict[str, Any]], List[Dict[str, str]], List[str]]:
|
2025-09-10 13:53:34 +08:00
|
|
|
"""从工作表提取毛入学率数据"""
|
2025-09-10 13:50:18 +08:00
|
|
|
enrollment_data: List[Dict[str, Any]] = []
|
|
|
|
conversion_records: List[Dict[str, str]] = []
|
|
|
|
name_conversion_errors: List[str] = []
|
|
|
|
|
|
|
|
# 计算区域名称列索引
|
|
|
|
region_col_idx = openpyxl.utils.column_index_from_string(REGION_NAME_COLUMN) - 1
|
|
|
|
|
|
|
|
# 遍历数据行
|
|
|
|
for row_num, row in enumerate(sheet.iter_rows(min_row=START_ROW, values_only=True), start=START_ROW):
|
|
|
|
# 获取区域名称
|
|
|
|
raw_name = row[region_col_idx] if (len(row) > region_col_idx and row[region_col_idx] is not None) else '未知地区'
|
|
|
|
|
2025-09-10 13:53:34 +08:00
|
|
|
# 转换区域名称
|
|
|
|
area_name, area_code, conv_records, errors = convert_area_name(raw_name, row_num)
|
|
|
|
conversion_records.extend(conv_records)
|
|
|
|
name_conversion_errors.extend(errors)
|
2025-09-10 13:50:18 +08:00
|
|
|
|
2025-09-10 13:53:34 +08:00
|
|
|
if not area_name:
|
|
|
|
continue
|
2025-09-10 13:50:18 +08:00
|
|
|
|
|
|
|
# 创建区域数据对象
|
2025-09-10 11:15:03 +08:00
|
|
|
area_data = {
|
|
|
|
'area_name': area_name,
|
|
|
|
'area_code': area_code,
|
2025-09-10 13:53:34 +08:00
|
|
|
'raw_name': str(raw_name).strip() if raw_name else '未知地区'
|
2025-09-10 11:15:03 +08:00
|
|
|
}
|
2025-09-10 13:50:18 +08:00
|
|
|
|
2025-09-10 11:15:03 +08:00
|
|
|
# 提取各指标年度数据
|
2025-09-10 13:50:18 +08:00
|
|
|
for metric, config in DATA_COLUMNS.items():
|
2025-09-10 11:15:03 +08:00
|
|
|
year_data = {}
|
|
|
|
if 'columns' in config and 'years' in config:
|
|
|
|
for col_name, year in zip(config['columns'], config['years']):
|
|
|
|
col_idx = openpyxl.utils.column_index_from_string(col_name) - 1
|
|
|
|
if col_idx < len(row):
|
|
|
|
value = row[col_idx]
|
2025-09-10 13:50:18 +08:00
|
|
|
year_data[str(year)] = process_value(value)
|
2025-09-10 11:15:03 +08:00
|
|
|
area_data[metric] = year_data
|
2025-09-10 13:50:18 +08:00
|
|
|
|
2025-09-10 11:15:03 +08:00
|
|
|
enrollment_data.append(area_data)
|
2025-09-10 13:50:18 +08:00
|
|
|
|
|
|
|
return enrollment_data, conversion_records, name_conversion_errors
|
2025-09-10 11:15:03 +08:00
|
|
|
|
2025-09-10 13:50:18 +08:00
|
|
|
# ======================= 主函数 =======================
|
|
|
|
def main() -> None:
|
|
|
|
"""主函数:执行毛入学率数据提取流程"""
|
2025-09-10 13:53:34 +08:00
|
|
|
init_directories(DATA_DIR)
|
|
|
|
|
|
|
|
# 加载工作表
|
|
|
|
sheet = load_workbook_sheet(EXCEL_PATH, SHEET_NAME)
|
|
|
|
if not sheet:
|
|
|
|
return
|
|
|
|
|
|
|
|
# 提取数据
|
|
|
|
enrollment_data, conversion_records, name_conversion_errors = extract_enrollment_data(sheet)
|
|
|
|
|
|
|
|
# 保存结果
|
|
|
|
save_to_json(enrollment_data, JSON_PATH)
|
|
|
|
|
|
|
|
# 输出转换统计
|
|
|
|
print_conversion_stats(conversion_records, name_conversion_errors)
|
2025-09-10 11:15:03 +08:00
|
|
|
|
2025-09-10 13:50:18 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2025-09-10 11:15:03 +08:00
|
|
|
|