python提取word题库标题、答案
时间: 2023-09-16 07:09:03 浏览: 211
您可以使用Python中的`python-docx`库来提Word题库的标题和答案。首先,您需要安装该库,可以使用以下命令进行安装:
```
pip install python-docx
```
然后,您可以使用以下代码来提取Word题库的标题和答案:
```python
from docx import Document
def extract_questions(file_path):
doc = Document(file_path)
questions = []
for paragraph in doc.paragraphs:
text = paragraph.text.strip()
if text.startswith("Question:"):
question = {"title": text.replace("Question:", "").strip()}
answers = []
for i in range(1, 5):
answer = doc.paragraphs[paragraph.number + i].text.strip()
answers.append(answer)
question["answers"] = answers
questions.append(question)
return questions
# 调用函数并传入题库文件路径
questions = extract_questions("题库.docx")
# 打印题目标题和答案
for question in questions:
print("Question:", question["title"])
print("Answers:", question["answers"])
print()
```
请将`"题库.docx"`替换为您实际的题库文件路径。以上代码将提取Word题库中以"Question:"开头的段落作为题目标题,并将紧随其后的四个段落作为答案选项。您可以根据实际需要进行修改和优化。
阅读全文