快搜搜题标准题库转换

发布时间:2026/8/1 10:27:19
快搜搜题标准题库转换 可将多个无法导入的题库转换为1个可直接导入识别的标准题库。1、Excel表格格式转换先批量将xls格式的题库转为xlsx格式的题库import os import xlrd from openpyxl import Workbook def main(input_folder, output_folder): os.makedirs(output_folder, exist_okTrue) for file in os.listdir(input_folder): if file.endswith(.xls) and not file.startswith(~): xls_path os.path.join(input_folder, file) xlsx_path os.path.join(output_folder, file.replace(.xls, .xlsx)) wb_xls xlrd.open_workbook(xls_path) wb_xlsx Workbook() wb_xlsx.remove(wb_xlsx.active) for sheet_idx in range(wb_xls.nsheets): sheet_xls wb_xls.sheet_by_index(sheet_idx) ws wb_xlsx.create_sheet(titlesheet_xls.name) for r in range(sheet_xls.nrows): ws.append([sheet_xls.cell(r, c).value for c in range(sheet_xls.ncols)]) wb_xlsx.save(xlsx_path) print(f✔ {file}) print(✅转换完成) if __name__ __main__: input_folder rC:\Users\Anperlanch Xie\Desktop\考试题库-xls output_folder rC:\Users\Anperlanch Xie\Desktop\考试题库-xlsx main(input_folder, output_folder)2、转为适配快搜搜题的标准题库格式重新排列并分割选项将简答题也转为可识别的选择题import os import re from openpyxl import load_workbook, Workbook def parse_options(opt_str): if opt_str is None or not isinstance(opt_str, str): return [] parts opt_str.split(|) options [] for part in parts: part part.strip() if not part: continue m re.match(r^[A-Za-z]-, part) if m: content part[m.end():].strip() else: content part options.append(content) return options def process_file(file_path, target_ws): try: wb load_workbook(file_path, data_onlyTrue) ws wb.worksheets[0] except Exception as e: print(f无法读取文件 {os.path.basename(file_path)}: {e}) return headers [cell.value for cell in ws[1]] try: col_question headers.index(题干) col_options headers.index(选项) col_answer headers.index(答案) except ValueError: print(f文件 {os.path.basename(file_path)} 缺少必要列跳过) return for row in ws.iter_rows(min_row2, values_onlyTrue): question row[col_question] answer row[col_answer] opt_str row[col_options] if not question: continue if opt_str \\: opts [answer] else: opts parse_options(opt_str) while len(opts) 6: opts.append() opts opts[:6] if opt_str \\: new_row [question, A] opts else: new_row [question, answer] opts target_ws.append(new_row) print(f✅已处理: {os.path.basename(file_path)}) def batch_convert(folder_path, output_path): wb_out Workbook() ws_out wb_out.active out_headers [题干, 答案, 选项A, 选项B, 选项C, 选项D, 选项E, 选项F] ws_out.append(out_headers) files [f for f in os.listdir(folder_path) if f.endswith(.xlsx) and f ! os.path.basename(output_path)] if not files: print(未找到任何.xlsx文件请先转换文件格式) return print(f共找到 {len(files)} 个文件开始处理...) for file in files: file_path os.path.join(folder_path, file) process_file(file_path, ws_out) ws_out.column_dimensions[A].width 65 for col_letter in [B, C, D, E, F, G, H]: ws_out.column_dimensions[col_letter].width 20 wb_out.save(output_path) print(f全部完成合并文件保存至{output_path}) if __name__ __main__: folder rC:\Users\Anperlanch Xie\Desktop\考试题库-xlsx output rC:\Users\Anperlanch Xie\Desktop\标准安全考试题库.xlsx batch_convert(folder, output)