吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1000|回复: 4
收起左侧

[Python 原创] xls试题库转word文档

  [复制链接]
hdbb 发表于 2025-5-20 16:37
多选试题库转换工具使用说明

## 📌 工具概述
**XLS多选题转Word工具**是一款专为教育工作者设计的批量文档转换工具,可将Excel格式的多选题题库自动转换为规范化的Word文档格式。

## 🌟 核心功能
### 1. 智能格式转换
- ✅ 支持`A-E`五个选项的多选题
- ✅ 自动识别并跳过空白选项(如E选项为空时自动隐藏)
- ✅ 保留原题库中的题目编号

### 2. 答案标准化处理
```diff
+ 输入支持:ABD / A,D,B / A,D,B  
+ 输出统一:A,B,D(自动排序去重)[/md]

必须xls

[Asm] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
"""
XLS多选题转Word工具(支持A-E选项,兼容空选项)
输入格式要求:
第1列:题目
第2-6列:A-E选项(允许E为空)
第7列:答案(如:ABD 或 A,C,E)
输出格式:
题目内容
A. 选项A
B. 选项B
...
E. 选项E(若无则省略)
答案:A,B,D
"""
 
import os
from xlrd import open_workbook
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
 
def safe_convert(cell_value):
    """安全转换单元格内容为字符串"""
    if cell_value is None:
        return ""
    if isinstance(cell_value, (float, int)):
        # 处理整数和小数
        return str(int(cell_value)) if cell_value.is_integer() else str(cell_value)
    return str(cell_value).strip()
 
def format_answer(ans):
    """标准化答案格式(ABD → A,B,D)"""
    ans = ans.replace(',', '').replace(',', '').upper()
    return ','.join(sorted(set(ans)))  # 去重排序
 
def xls_to_word(input_path, output_path):
    try:
        wb = open_workbook(input_path)
        sheet = wb.sheet_by_index(0)
         
        doc = Document()
        doc.styles['Normal'].font.name = '宋体'
        doc.styles['Normal'].font.size = Pt(12)
         
        for row_idx in range(1, sheet.nrows):
            row = sheet.row(row_idx)
             
            # 处理题目和选项
            question = safe_convert(row[0].value) or "【题目缺失】"
            options = [
                safe_convert(row[i].value) for i in range(1, 6)  # A-E选项
            ]
            raw_answer = safe_convert(row[6].value) if len(row) > 6 else ""
            answer = format_answer(raw_answer) if raw_answer else "【答案缺失】"
             
            # 添加题目(自动编号)
            p_question = doc.add_paragraph()
            p_question.add_run(f"{row_idx}. {question}").bold = True
             
            # 添加选项(跳过空选项)
            option_labels = ['A', 'B', 'C', 'D', 'E']
            for label, content in zip(option_labels, options):
                if content:  # 只输出非空选项
                    doc.add_paragraph(f"{label}. {content}")
             
            # 添加答案(蓝色+右对齐)
            p_answer = doc.add_paragraph()
            p_answer.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
            answer_run = p_answer.add_run(f"答案:{answer}")
            answer_run.font.color.rgb = RGBColor(0, 0, 255)  # 蓝色
             
            doc.add_paragraph()  # 题目间距
         
        doc.save(output_path)
        print(f"转换成功:{os.path.basename(input_path)} → {os.path.basename(output_path)}")
        return True
     
    except Exception as e:
        print(f"转换失败:{input_path}\n错误类型:{type(e).__name__}\n错误详情:{str(e)}")
        return False
 
if __name__ == "__main__":
    print("="*50)
    print("XLS多选题转Word工具".center(40))
    print("="*50)
     
    input_file = input("拖入.xls文件或输入路径:").strip('"')
    if not input_file.lower().endswith('.xls'):
        print("错误:仅支持.xls格式文件")
    else:
        output_file = os.path.splitext(input_file)[0] + "_转换结果.docx"
        xls_to_word(input_file, output_file)
     
    input("\n按回车键退出...")

convert.zip

1.68 KB, 下载次数: 34, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| hdbb 发表于 2025-5-20 16:40
本来写好了,我删了一个附件的原因?文本框全乱了。就用ai写了。现在ai真厉害。傻瓜式操作了。
yiluoen0502 发表于 2025-5-21 08:49
sdlkga 发表于 2025-6-24 19:28
 楼主| hdbb 发表于 2025-7-8 09:08

用处不大。完全可以临时根据题库做。深度思考都不用开。我现在又做了可以加载解析的。混存在感的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-9-19 06:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表