- 积分
- 4457
- 威望
- 290 点
- 活跃
- 5825 点
- 好友
- 2
- BLG币
- 5646 钱
- 阅读权限
- 80
- 注册时间
- 2008-9-19
- 最后登录
- 2022-10-12
情理徔ф,噫料徔外
- UID
- 12660
- 主题
- 130
- 精华
- 0
- 帖子
- 6800
- 积分
- 4457
- 活跃
- 5825 点
- 威望
- 290 点
- 签到
- 127 天
- BLG币
- 5646 钱
- 筹码
- 0 枚
- 在线时间
- 1304 小时
- 注册时间
- 2008-9-19
- 最后登录
- 2022-10-12

现金: $-37407117 名声: 7 称号: 游民 进入游戏
|

楼主 |
发表于 2022-3-21 16:50
|
显示全部楼层
本帖最后由 布拉格の蓝颜 于 2022-3-21 21:58 编辑
# -*- 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'{val1} {randomSymbol} {val2} ='
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'''
{val1} {val2} {val3}
{randomSymbol}{val4} {val5} {val6}
'''
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') |
|