热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python十以内加减法_python生成PDF文件20以内加减法,给上小学的宝宝

#-*-coding:utf-8-*-importrandomfromreportlabimportplatypusfromreportlab.lib.unitsimportinc

# -*- coding: utf-8 -*-

import random

from reportlab import platypus

from reportlab.lib.units import inch

from reportlab.lib.pagesizes import A4, landscape #最后生成横向打印的PDF

from reportlab.lib import colors

def genList():

ret = set()

while True:

a = random.randint(1, 20)

b = random.randint(1, 20)

c = random.randint(1, 20)

if a &#43; b &#43; c <&#61; 20 and (a > 10 or b > 10 or c > 10):

ret.add( &#39;%-2d &#43; %-2d &#43; %-2d &#61;&#39; % (a, b, c) )

if len(ret) &#61;&#61; 25: break

while True:

a &#61; random.randint(1, 20)

b &#61; random.randint(1, 20)

c &#61; random.randint(1, 20)

if a &#43; b <&#61; 20 and a &#43; b - c >&#61; 0 and (a > 10 or b > 10 or c > 10):

ret.add( &#39;%-2d &#43; %-2d - %-2d &#61;&#39; % (a, b, c) )

if len(ret) &#61;&#61; 50: break

while True:

a &#61; random.randint(1, 20)

b &#61; random.randint(1, 20)

c &#61; random.randint(1, 20)

if a - b >&#61; 0 and a - b &#43; c <&#61; 20 and (a > 10 or b > 10 or c > 10):

ret.add( &#39;%-2d - %-2d &#43; %-2d &#61;&#39; % (a, b, c) )

if len(ret) &#61;&#61; 75: break

while True:

a &#61; random.randint(11, 20)

b &#61; random.randint(1, 20)

c &#61; random.randint(1, 20)

if a - b - c >&#61; 0 and (a > 10 or b > 10 or c > 10):

ret.add( &#39;%-2d - %-2d - %-2d &#61;&#39; % (a, b, c) )

if len(ret) &#61;&#61; 100: break

ret &#61; list(ret)

random.shuffle(ret)

return ret

&#39;&#39;&#39;for i in genList():

print i&#39;&#39;&#39;

def genTable():

data &#61; []

items &#61; genList()

for i in range(0, len(items), 5):

data.append(items[i:i&#43;5])

# Courier 是 reportlab 缺省定义的等宽字体

# (0,0)/(-1,-1)说的是style运用范围&#xff0c;从左上到右下

tablestyle &#61; [(&#39;FONT&#39;, (0,0), (-1,-1), &#39;Courier&#39;, 12),

(&#39;LINEAFTER&#39;, (0,0), (-2,-1), 1, colors.black)]

return platypus.Table(data, 2.2*inch, 0.35*inch, tablestyle)

import sys

try:

n &#61; int(sys.argv[1]) #恩恩&#xff0c;生成一个多页的PDF

except:

n &#61; 1

try:

fname &#61; sys.argv[2]

except:

fname &#61; &#39;20plus100.pdf&#39;

doc &#61; platypus.SimpleDocTemplate(fname, topMargin&#61;0.5*inch, bottomMargin&#61;0.5*inch, title&#61;&#39;DaDa Math&#39;, author&#61;&#39;qyb&#39;)

elements &#61; []

for i in range(n):

elements.append(genTable())

elements.append(platypus.flowables.PageBreak())

doc.pagesize &#61; landscape(A4)

doc.build(elements)

这个 功能需要用到python  的reportlab  包&#xff0c;&#xff0c;请下载安装

分享到&#xff1a;

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-12-24 16:00

浏览 3765

评论



推荐阅读
author-avatar
MINT米田
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有