代码拉取完成,页面将自动刷新
# -*- 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(10,999)
val2 = random.randint(10,999)
randomSymbol = plusOrminus[random.randint(0,2)]
if (randomSymbol == '-'):
val2 = random.randint(10,val1)
if (randomSymbol == '+'):
while val1+val2 > 999:
val1 = random.randint(10,999)
val2 = random.randint(10,999)
if (randomSymbol == '×'):
while val1*val2 > 998000:
val1 = random.randint(10,999)
val2 = random.randint(10,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(10,999)
valm = random.randint(10,999)
valstr = ""
randomSymbol = plusOrminus[random.randint(0,2)]
# 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 == '-'):
valm = random.randint(10,valn)
if (randomSymbol == '+'):
while valn+valm > 999:
valn = random.randint(10,999)
valm = random.randint(10,999)
if (randomSymbol == '×'):
while valn*valm > 998000:
valn = random.randint(10,999)
valm = random.randint(10,999)
strn = str(valn)
strm = str(valm)
if valn > 100:
val1 = strn[-3]
else:
val1 = " "
# 半角空格: https://blog.csdn.net/u014781844/article/details/84859693
val2 = strn[-2]
val3 = strn[-1]
if valm > 100:
val4 = strm[-3]
else:
val4 = " "
# 半角空格: https://blog.csdn.net/u014781844/article/details/84859693
val5 = strm[-2]
val6 = strm[-1]
# if (randomSymbol == '-'):
# val4 = valstr[0] if valstr[0]!='0' else " "
# 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="multilines">
<div></div>
</div>
</li>'''
list.append(str)
return list
def generate_html(practice1,practice2):
env = Environment(loader=FileSystemLoader('./'))
template = env.get_template('template2.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):
# 设置环境变量
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("输入生成多少张试卷:"))
practice1Num = int(input("每张试卷 口算练习 题目数量:"))
practice2Num = int(input("每张试卷 竖式计算 题目数量:"))
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}张试卷......")
practice1 = initPractice1(practice1Num)
practice2 = initPractice2(practice2Num)
generate_html(practice1,practice2)
asyncio.get_event_loop().run_until_complete(html2pdf(time_ext + "_" + str(i+1)))
print("全部生成完毕!按任意键退出程序")
os.system('pause')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。