m0_64570815
2022-05-21 16:01
采纳率: 100%
浏览 45
已结题

如何通过python提取word里面的选择题和填空题

import pandas as pd@
import re
from docx import Document
from collections import OrderedDict

doc = Document("体育.docx")

black_char = re.compile("[\s\u3000\xa0]+")

chinese_nums_rule = re.compile("[一二三四]、(.+?)(")
title_rule = re.compile("\d+.")
option_rule = re.compile("([ABCDEF])")
option_rule_search = re.compile("([ABCDEF])[^(]+")

保存最终的结构化数据

question_type2data = OrderedDict()

从word文档的“一、单项选择题”开始遍历数据

for paragraph in doc.paragraphs[5:]:
# 去除空白字符,将全角字符转半角字符,并给括号之间调整为中间一个空格
line = black_char.sub("", paragraph.text).replace(
"(", "(").replace(")", ")").replace(".", ".").replace("()", "( )")
# 对于空白行就直接跳过
if not line:
continue
if title_rule.match(line):
options = title2options.setdefault(line, [])
elif option_rule.match(line):
options.extend(option_rule_search.findall(line))
else:
chinese_nums_match = chinese_nums_rule.match(line)
if chinese_nums_match:
question_type = chinese_nums_match.group(1)
title2options = question_type2data.setdefault(
question_type, OrderedDict())

result = []
max_options_len = 0
for question_type, title2options in question_type2data.items():
for title, options in title2options.items():
result.append([question_type, title, *options])
options_len = len(options)
if options_len > max_options_len:
max_options_len = options_len
df = pd.DataFrame(result, columns=[
"题型", "题目"]+[f"选项{i}"for i in range(1, max_options_len+1)])

题型可以简化下,去掉选择两个字

df['题型'] = df['题型'].str.replace("选择", "")
df.to_excel("result.xlsx", index=False)

为什么会报错

1条回答 默认 最新

相关推荐 更多相似问题