本帖最后由 dreamrise 于 2021-11-21 21:25 编辑
源自: https://www.52pojie.cn/thread-1538498-1-1.html 作者: 深山霜枫
闲的无聊,扩展了乘除功能, 目前支持 2-3位的加减乘除,横式与竖式
除法竖式用到了:https://gitee.com/tmnet/formula
继续开源.完整内容在: https://gitee.com/dreamrise/mathgen
输出结果请看output下面的样本.
[Python] 纯文本查看 复制代码 # -*- 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(10,100)
randomSymbol = plusOrminus[random.randint(0,1)]
if (randomSymbol == '÷'):
# 除法用乘法反向出题 :
# 999*999=998001 以内的除法
while val1*val2 > 998000:
val1 = random.randint(100,999)
val2 = random.randint(100,999)
val1 = val1*val2 # 用乘积做除数
val2 = val2 # 用其中一个数做除数
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 multiply(num):
list = []
plusOrminus = ['×']
for i in range(num):
from builtins import str
valn = random.randint(100,999)
valm = random.randint( 10,200)
valstr = ""
randomSymbol = plusOrminus[0]
strn = str(valn)
strm = str(valm)
val1 = strn[0]
val2 = strn[1]
val3 = strn[2]
if valm > 100:
val4 = strm[-3]
else:
val4 = " "
# 半角空格: https://blog.csdn.net/u014781844/article/details/84859693
val5 = strm[-2]
val6 = strm[-1]
string = f'''<li>
<div class="top">
{val1} {val2} {val3}
</div>
<div class="middle">
<span>{randomSymbol}</span>{val4} {val5} {val6}
</div>
<div class="multilines">
<div></div>
</div>
</li>'''
list.append(string)
return list
def division(num):
list = []
for i in range(num):
val1 = random.randint(100,999)
val2 = random.randint(100,999)
while val1*val2 > 998000:
val1 = random.randint(100,999)
val2 = random.randint(100,999)
valn = val1*val2 # 用乘积做除数
valm = val2 # 用其中一个数做除数
strn = str(valn)
strm = str(valm)
string = f'''<li>
<div class="top">
<division>{strn}/{strm}</division>
</div>
<div class="division">
<div></div>
</div>
</li>'''
list.append(string)
return list;
def generate_html(practice2,practice3):
env = Environment(loader=FileSystemLoader('./'))
template = env.get_template('template乘除竖式.html')
with open("result.html",'w',encoding="utf-8") as fout:
html_content = template.render(practice2=practice2,practice3=practice3)
fout.write(html_content)
async def html2pdf(fileName):
# 设置环境变量
os.environ['no_proxy']="localhost,127.0.0.1"
browser = await launch()
# browser = await launch(options={'args': ['--no-sandbox']})
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("输入生成多少张试卷:"))
multiplyNum = int(input("每张试卷 竖式计算 乘法题目数量:"))
divisionnum = int(input("每张试卷 竖式计算 除法题目数量:"))
# count, multiplyNum,divisionnum = 1, 8, 8
import time
time_ext = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))
for i in range(count):
print(f"正在生成第{i+1}张试卷......")
practice2 = multiply(multiplyNum)
practice3 = division(divisionnum)
generate_html(practice2,practice3)
asyncio.get_event_loop().run_until_complete(html2pdf(time_ext + "_" + str(i+1)))
print("全部生成完毕!按任意键退出程序")
os.system('pause')
|