Python : 在报告实验室顶部添加徽标

Python : Add Logo on top in reportlab

我在 python-2.7 中使用 reportlab 生成 PDF。我正在尝试使用此代码在 PDF 的左上角添加徽标。

Story = []
logo = "logo.png"
im = Image(logo, 1 * inch, 1 * inch)
t = Story.append(im)

但它没有将 logo.png 图像显示为 PDF。谁能告诉我我哪里出错了?

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle, Image
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
from reportlab.lib.units import inch

document = []
doc = SimpleDocTemplate('example.pdf', pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72)
styles = getSampleStyleSheet()

Story = []
logo = "logo.png"
im = Image(logo, 1 * inch, 1 * inch)
t = Story.append(im)

definitions = []
i, a = 1, 65
table = []
for x in range(1, 10):
    line = []
    line.append(Paragraph(str(i), styles['BodyText']))
    line.append(Paragraph('Vocabulary', styles['BodyText']))
    line.append(Paragraph(chr(a), styles['BodyText']))
    line.append(Paragraph('Often a multi-line definition of the vocabulary. But then, sometimes something short and sweet.', styles['BodyText']))
    table.append(line)
    i += 1
    a += 1

t = Table(table, colWidths=(1*cm, 4*cm, 1*cm, None))
t.setStyle(TableStyle([
    ('VALIGN', (1, 1), (-1, -1), 'TOP')
]))

document.append(t)
doc.build(document)

您需要将徽标附加到 document,您将从中 build()

im = Image(logo, 1 * inch, 1 * inch)之后添加document.append(im)

有很好的教程here