#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import json from openpyxl import load_workbook def convert_excel_to_json(): """将合并题库.xlsx转换为JSON格式""" excel_file = '/Users/moshui/Documents/survey/output/合并题库.xlsx' json_file = '/Users/moshui/Documents/survey/public/questions.json' try: # 读取Excel文件 wb = load_workbook(excel_file) ws = wb['单选题'] questions = { "基础题": [], "进阶题": [], "竞赛题": [] } # 获取表头 headers = [] for cell in ws[1]: headers.append(cell.value) # 读取数据行 for row in ws.iter_rows(min_row=2, values_only=True): if len(row) < len(headers): continue question_data = {} for i, header in enumerate(headers): if header and i < len(row): question_data[header] = row[i] if row[i] is not None else "" # 跳过空题目 if not question_data.get('题干', '').strip(): continue # 确定题目类型(基于分数) score = int(question_data.get('分数', 5)) if score == 5: question_type = "基础题" elif score == 10: question_type = "进阶题" elif score == 15: question_type = "竞赛题" else: question_type = "基础题" # 默认 # 添加题目元数据 question_data['题目类型'] = question_type question_data['题目标签'] = question_data.get('标签', '').strip() questions[question_type].append(question_data) # 保存为JSON文件 with open(json_file, 'w', encoding='utf-8') as f: json.dump(questions, f, ensure_ascii=False, indent=2) print(f"成功转换题库,共:") print(f" 基础题: {len(questions['基础题'])} 道") print(f" 进阶题: {len(questions['进阶题'])} 道") print(f" 竞赛题: {len(questions['竞赛题'])} 道") print(f"JSON文件已保存到: {json_file}") return questions except Exception as e: print(f"转换失败: {str(e)}") return None if __name__ == "__main__": convert_excel_to_json()