python处理word文档,如何提取文档中的题目与答案

简介: python处理word文档,如何提取文档中的题目与答案

python处理word文档,如何提取文档中的题目与答案

需求分析

文档格式和题目格式如下,就是需要写出一个对象,然后可以提取出这个文档里面,题目,答案,组号,然后封装成一个对象。

在古代中国社会中,私学是与官学相对而存在的,并且在中国教育上占有重要的地位。我国历史上_______私学规模最大、影响最深远。
a、孔子
b、孟子
c、老子
d、墨子
答案:A 
组号:1

具体代码

class Question:
    def __init__(self, id, text, options, answer):
        self.id = id
        self.text = text
        self.options = options
        self.answer = answer
    def __str__(self):
        return f'{self.id}. {self.text} 选项: {self.options} 答案: {self.answer}'
def extract_question_option(text):
    questions = []
    options = []
    answers = []
    lines = text.split('\n')
    # 预处理:去掉空行并将多行题目合并为一行
    for i in range(len(lines)):
        line = lines[i].strip()
        if not line:
            continue
        if i < len(lines) - 1 and lines[i + 1].startswith(('答案:', '组号:')):
            # 当前行不是题目的最后一行,将其添加到前一个题目中
            if len(questions) > 0:
                questions[-1] += line
        else:
            # 当前行是题目的最后一行,将其添加为新的题目
            questions.append(line)
    # 处理选项和答案
    current_question_idx = -1
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line.startswith('组号:'):
            # 处理组号
            group_id = line.split(':')[1].strip()
            current_question_idx = 0
        elif line.startswith('答案:'):
            # 处理答案
            answer = line.split(':')[1].strip()
            if current_question_idx >= 0 and current_question_idx < len(questions):
                # 创建新的 Question 对象并添加到列表中
                question = Question(current_question_idx + 1, questions[current_question_idx], options[current_question_idx], answer)
                questions[current_question_idx] = question
            current_question_idx += 1
        else:
            # 处理选项
            options_list = line.split('、')
            options = [opt.strip() for opt in options_list]
            if len(options) > 0:
                # 处理非空选项
                if current_question_idx >= 0 and current_question_idx < len(questions):
                    options.append(options)
            else:
                # 处理空选项(没有填写选项)
                if current_question_idx >= 0 and current_question_idx < len(questions):
                    options.append([])
    return questions
if __name__ == '__main__':
    # 读取 Word 文档并解析题目
    file_path = "D:\\系统默认\\桌面\\测试题目-三组.docx"
    text = read_word_document(file_path)
    questions = extract_question_option(text)
    # 遍历题目并打印结果
    for question in questions:
        print(question)

运行结果:

相关文章
|
2月前
|
XML 关系型数据库 MySQL
python将word(doc或docx)的内容导入mysql数据库
用python先把doc文件转换成docx文件(这一步也可以不要后续会说明),然后读取docx的文件并另存为htm格式的文件(上一步可以直接把doc文件另存为htm),python根据bs4获取p标签里的内容,如果段落中有图片则保存图片。(图片在word文档中的位置可以很好的还原到生成的数据库内容) 我见网上有把docx压缩后解压获取图片的,然后根据在根据xml来读取图片的位置,我觉得比较繁琐。用docx模块读取段落的时候还需要是不是判断段落中有分页等,然而转成htm之后就不用判断那么多直接判断段落里的样式或者图片等就可以了。
31 1
|
2月前
|
测试技术 API 数据处理
Python办公自动化:解锁高效工作流程,掌握文档处理的艺术
Python办公自动化:解锁高效工作流程,掌握文档处理的艺术
87 1
|
3月前
|
机器学习/深度学习 算法 数据挖掘
python高级在线题目训练-第二套·主观题
python高级在线题目训练-第二套·主观题
43 0
|
7天前
|
存储 SQL 缓存
阿里云大学考试python中级题目及解析-python中级
阿里云大学考试python中级题目及解析-python中级
15 0
|
16天前
|
BI 开发者 数据格式
Python代码填充数据到word模板中
【4月更文挑战第16天】
|
16天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
17天前
|
Python
Python 的编码规范和最佳实践: 解释 Python 的文档字符串(docstring)是什么?如何编写好的文档字符串?
【4月更文挑战第16天】Python docstrings是注释,用于说明代码功能。放置于对象定义前,用三引号包围。遵循PEP 257,使用reStructuredText格式,确保简洁、完整、准确。例如: ```markdown ```python def add(a, b): """ 计算两数之和。 参数: a -- 第一加数 b -- 第二加数 返回: 和 """ return a + b ``` ```
15 0
|
25天前
|
安全 数据安全/隐私保护 Python
292: 程序设计C 实验五 题目三 设计密码(python)
292: 程序设计C 实验五 题目三 设计密码(python)
|
2月前
|
Python
Python教程:如何向Word中添加表格
Microsoft Word是一种流行的文档处理软件,广泛用于创建各种类型的文档,包括报告、简历、手册等。Python提供了许多库来处理Microsoft Word文档,其中包括`python-docx`,它使我们能够轻松地创建、修改和操作Word文档。本文将介绍如何使用Python的`python-docx`库向Word文档中添加表格。
20 0
|
2月前
|
XML 存储 算法
使用Python的zipfile模块巧解Word批量生成问题
使用Python的zipfile模块巧解Word批量生成问题
20 0