【发布时间】:2014-03-10 02:18:05
【问题描述】:
我正在编写一个小程序来制作一堆 BINGO 卡片。
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, letter
from reportlab.lib.units import inch
import random
def set_ranges():
r = {
"b": [1, 15, 0],
"i": [16, 30, 0],
"n": [31, 45, 0],
"g": [46, 60, 0],
"o": [61, 75, 0]
}
return r
def set_canvas():
c = canvas.Canvas("bingo.pdf")
c.setPageRotation(90)
c.setFont('Helvetica-Bold', 14)
print type(c)
return c
def print_card(ranges, canvas):
# Set a page gutter
gutter = 1 * inch
x = gutter
y = 8.5 * inch - 2 * gutter
# First draw the letters themselves
for letter in "bingo":
canvas.drawString(x, y, letter)
# Print X and Y to troubleshoot
# print('%s, %d, %d' % (letter, x, y))
# Add the X value for each letter to the dictionary
ranges[letter][2] = x
x = x + 1 * inch
y_reset = y - 1 * inch
# Then pull the numbers for each square
for letter in "bingo":
row = random.sample(range(ranges[letter][0], ranges[letter][1] + 1), 5)
if letter == "n":
row[2] = "FREE"
x = ranges[letter][2]
y = y_reset
for col in row:
# Print X and Y to troubleshoot
# print('%d, %d, %d' % (col, x, y))
canvas.drawString(x, y, str(col))
y = y - 1 * inch
canvas.save()
我还有一些工作要做(“FREE”应该居中!我需要画线)但这基本上是可行的。我用r = set_ranges() 和c = set_canvas() 然后for i in range (1,25): print_card(r,c) 来创建一个包含基本卡片的PDF。
但是在第一页之后,字体不再是粗体。它会在哪里重置?
【问题讨论】:
-
(大部分)最终产品,如果您关心的话:gist.github.com/amandabee/8969833
标签: python pdf-generation reportlab