我的字体在哪里重置?

2024-09-27 04:08:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个小程序来制作一堆宾果卡。你知道吗

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()

我还有一些工作要做(“自由”应该是中心的!我需要画线)但这基本上是可行的。我做r = set_ranges()c = set_canvas()然后for i in range (1,25): print_card(r,c)来创建一个包含基本卡片的PDF文件。你知道吗

但在第一页之后,字体就不再粗体了。会在哪里重置?你知道吗


Tags: theinfromimportfordefcanvasprint
2条回答

从reportlab参考:

def save(self): Saves and close the PDF document in the file. If there is current data a ShowPage is executed automatically. After this operation the canvas must not be used further.

所以你的代码工作起来很奇怪。只需生成所有表,然后调用canvas.save()。你知道吗

您正在使用哪个版本的ReportLab?版本1.1.0(2012-12-18)有一个错误,在呈现前重置字体有时。。。你知道吗

相关问题 更多 >

    热门问题