【小学生作业自动生成】自动生成小学加减法作业

4次阅读
没有评论

简易自动生成小学生作业【开源】

自动生成三位数以内的小学生加减法作业,并生成A4大小的PDF文件便于打印,效果如图所示:

便捷使用

  • 下载已经编译好的程序 点我直达下载
  • 解压文件,注意解压出来的四个文件必须在同一文件夹下
  • 运行homeworkProduce.exe,第一次运行会自动下载Chrome driver
  • 根据提示填入需要生成多少张作业,每张试卷口算练习题数量,竖式计算题数量,然后回车
  • output文件夹下会生成指定数量张的作业

实现思路

  • 作业模板基于HTML+CSS,使用jinja2模板引擎动态渲染数据传入HTML
  • 基于Chrome headlessApiHTML打印成PDF,这里使用Pyppeteer

自行编译

  • 支持Python3.6Python3.7Python3.8Python3.9、注意WIn7不支持Python3.8以上
  • pip install jinja2
    pip install pyppeteer

代码写得很烂

# -*- coding: utf-8 -*-
from jinja2 import Environment, FileSystemLoader
import random
import asyncio
import os
from pyppeteer import launch
def initPractice1(num):
list = []
plusOrminus = ['+','-']
for i in range(num):
val1 = random.randint(100,999)
val2 = random.randint(100,999)
randomSymbol = plusOrminus[random.randint(0,1)]
if (randomSymbol == '-'):
val2 = random.randint(100,val1)
if (randomSymbol == '+'):
while val1+val2 > 999:
val1 = random.randint(100,999)
val2 = random.randint(100,999)
str = f'<li>{val1} {randomSymbol} {val2} =</li>'
list.append(str)
return list;
def initPractice2(num):
list = []
plusOrminus = ['+','-']
for i in range(num):
from builtins import str
valn = random.randint(100,999)
valm = random.randint(100,999)
valstr = ""
randomSymbol = plusOrminus[random.randint(0,1)]
if (randomSymbol == '-'):
bfw = random.randint(0,int(valn/100));
sw = random.randint(0,(int(valn/10))%10);
gw = random.randint(0,valn%10)
valstr = str(bfw) + str(sw) + str(gw)
if (randomSymbol == '+'):
while valn+valm > 999:
valn = random.randint(100,999)
valm = random.randint(100,999)
strn = str(valn)
strm = str(valm)
val1 = strn[0]
val2 = strn[1]
val3 = strn[2]
val4 = strm[0]
val5 = strm[1]
val6 = strm[2]
if (randomSymbol == '-'):
val4 = valstr[0]
val5 = valstr[1]
val6 = valstr[2]
str = f'''<li>
<div class="top">
{val1} {val2} {val3}
</div>
<div class="middle">
<span>{randomSymbol}</span
>{val4} {val5} {val6}
</div>
<div class="down clearfix">
<div></div>
<div></div>
<div></div>
</div>
</li>'''
list.append(str)
return list
def generate_html(practice1,practice2):
env = Environment(loader=FileSystemLoader('./'))
template = env.get_template('template.html')
with open("result.html",'w',encoding="utf-8") as fout:
html_content = template.render(practice1=practice1,practice2=practice2)
fout.write(html_content)
async def html2pdf(fileName):
browser = await launch()
page = await browser.newPage()
await page.goto(os.path.join(os.getcwd(),"result.html"))
await page.pdf({
"path": "output/"+fileName+".pdf", "format": 'A4'})
await browser.close()
if __name__ == "__main__":
count = int(input("输入生成多少张试卷:"))
practice1Num = int(input("每张试卷 口算练习 题目数量:"))
practice2Num = int(input("每张试卷 竖式计算 题目数量:"))
for i in range(count):
print(f"正在生成第{i+1}张试卷......")
practice1 = initPractice1(practice1Num)
practice2 = initPractice2(practice2Num)
generate_html(practice1,practice2)
asyncio.get_event_loop().run_until_complete(html2pdf(str(i+1)))
print("全部生成完毕!按任意键退出程序")
os.system('pause')

正文完
 0
116博客
版权声明:本篇文章由 116博客 于2024-11-30发表,共计2624字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码