本文快速导读,如图所示:
一、生成试卷题目
老师出试卷的工作往往是比较费时的,那么,能否借助 AI 来快速生成试卷,再从中挑选合适的题目呢?我尝试了一下,发现效果很不错。
例如,我准备出一份初中一年级的期中试卷,我打开 DeepSeek 网站
🐳https://www.deepseek.com
点击“开始对话”,按照需求输入了以下提示词:
结合初中的教学大纲,给人教版出一份七年级的数学期中试卷,满分是120分,题量适中,给出完整的试卷题目。
很快,DeepSeek 就生成了一份期中考试的试卷。通过网页上最下方的复制小按钮,我将试卷内容复制出来,发现它是 MarkDown 格式的。
仔细查看后发现,这种格式无法直接复制到 Word 里使用,因为里面使用了 LaTeX 格式编写。
🐳
LaTeX 是一种专业的文档排版系统,广泛用于学术论文、数学公式、技术文档等场景。它的特点是能精准控制格式(尤其是数学符号和复杂公式),但需要特定的语法标记内容。
二、转换成 Word
为了将Markdown格式的试卷转换成 Word 文档,我尝试寻找解决方案,最终借助 Pandoc 实现了一键转换。但涉及到LaTex格式,原来拷贝出来的格式并不是通用的,需要转换成Pandoc方便处理的格式,所以首先使用python对Markdown进行LaTex公式的处理,然后再使用Pandoc转换生成Word文档。
以下是具体步骤:
步骤 1:安装依赖
我们需要安装必要的依赖。
1、安装 Pandoc:
对于 Windows 用户,访问 Pandoc 下载页面,下载并安装最新版本。
https://github.com/jgm/pandoc/releases
对于 Mac 用户,可以使用 Homebrew 安装:
brew install pandoc
2、安装 Python 依赖:
打开命令行终端,运行以下命令:
pip install argparse
步骤 2:准备脚本
1、创建一个新的 Python 文件,例如 md_to_word.py。
2、将以下代码复制到文件中:
import re
import subprocess
import argparse
import os
def prepare_markdown_for_export(content, convert_math=False):
if convert_math:
# 替换行内数学公式,包括相邻的空格
content = re.sub(r'\\\(\s*', '$', content)
content = re.sub(r'\s*\\\)', '$', content)
# 替换块级数学公式,包括相邻的空格
content = re.sub(r'\\\[\s*', '$$', content)
content = re.sub(r'\s*\\\]', '$$', content)
# 修复角度符号,只在特定情况下添加 \circ
def add_circ(match):
full_match = match.group(0)
if '$' in full_match: # 如果匹配内容在数学公式中,不做修改
return full_match
num = match.group(1)
exp = match.group(2)
if exp.strip() in ['1', '2', '3']: # 只处理 1, 2, 3 次方
return f"{num}^{exp}\\circ"
return full_match
# 使用负向预查确保不匹配数学公式内的内容
content = re.sub(r'(\d+)\^(\s*[123](?!\d))(?![^\$]*\$)', add_circ, content)
# 修复选择题格式,保持原有换行和空格
def fix_choices(match):
question = match.group(1)
choices = match.group(2)
return f"{question}\n{choices}"
content = re.sub(r'^(\d+\..+)$\n(\s+[A-D]\..+(?:\n\s+[A-D]\..+)*)',
fix_choices, content, flags=re.MULTILINE)
# 确保每行末尾有两个空格(Markdown 软换行)
content = re.sub(r'([^\n])$', r'\1 ', content, flags=re.MULTILINE)
# 保持原有的换行格式
content = re.sub(r'\n{3,}', r'\n\n', content)
return content
def convert_to_word(input_file, output_file):
command = f'pandoc -s {input_file} -o {output_file} --pdf-engine=xelatex --wrap=preserve'
subprocess.run(command, shell=True, check=True)
def main():
parser = argparse.ArgumentParser(description='Convert Markdown to Word with optional formula fixes.')
parser.add_argument('input', help='Input Markdown file')
parser.add_argument('output', help='Output Word file')
parser.add_argument('--convert-math', action='store_true', help='Convert math formulas to $ format')
parser.add_argument('--keep-md', action='store_true', help='Keep the prepared Markdown file')
args = parser.parse_args()
prepared_md = 'prepared.md'
# 读取输入文件
with open(args.input, 'r', encoding='utf-8') as file:
content = file.read()
# 准备 Markdown 内容
prepared_content = prepare_markdown_for_export(content, convert_math=args.convert_math)
# 写入准备好的 Markdown 文件
with open(prepared_md, 'w', encoding='utf-8') as file:
file.write(prepared_content)
# 转换为 Word 文档
convert_to_word(prepared_md, args.output)
if not args.keep_md:
os.remove(prepared_md)
print(f"转换完成:{args.output}")
if __name__ == "__main__":
main()
步骤 3:使用脚本
1、打开命令行终端。
2、导航到包含 md_to_word.py 脚本的目录。
3、运行以下命令来转换 Markdown 文件:
使用方法示例:
- 1. 只进行基本准备(不转换数学公式格式):
python md_to_word.py input.md output.docx
- 2. 进行基本准备并转换数学公式格式:
python md_to_word.py input.md output.docx --convert-math
- 3. 进行基本准备,转换数学公式格式,并保留准备好的 Markdown 文件:
python md_to_word.py input.md output.docx --convert-math --keep-md
把 Markdown 转换成 Word 文档后,可以看出已经变成可以打印的试卷格式,并且公式也正常展示出来。
需要注意的是,转换后的文档需要用微软的 Word 打开,这样公式才能直接编辑。如果用 WPS 打开,公式可能会不完整。
三、总结
AI 技术结合 Pandoc 的应用,为试卷生成和格式转换提供了一种高效、便捷的解决方案。当然,如果觉得自己安装依赖和执行脚本转换 Word 太麻烦了,下一期我分享使用网站生成 Word 的方案。
相关阅读
AI 应用实战|手把手教你用扣子(Coze)实现在一个在线 AI 翻译应用