# -*- 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;
2009-12-24 16:00
浏览 3765
评论