【问题标题】:Python : Add Logo on top in reportlabPython:在reportlab的顶部添加徽标
【发布时间】:2018-11-19 13:02:12
【问题描述】:

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

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

但它没有在 PDF 中显示logo.png 图像。有人可以告诉我我在哪里出错了吗?

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)

【问题讨论】:

  • @Mawg 它没有显示任何错误。但它没有在 PDF 中显示logo.png 图像
  • 你在用t = Story.append(im) 中的t 做什么?它似乎没有被使用,然后被t = Table(table, colWidths=(1*cm, 4*cm, 1*cm, None)) 覆盖 - 顺便说一句,养成使用有意义的变量名称的习惯。 6 个月后,你不会知道这是做什么的。
  • 也许你需要在t = Story.append(im)之后document.append(t)
  • @Mawg 感谢您的帮助。但它不起作用。
  • @Mawg 真的很抱歉。它正在工作。非常感谢

标签: python-2.7 reportlab


【解决方案1】:

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

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

有很好的教程here

【讨论】:

  • 请告诉我如何将徽标设置为左对齐?
  • 这是一个新问题,马丁。我们喜欢将其保留为一个问题和一个答案,否则它会变得混乱。发布一个新问题,但如果您愿意,可以链接到这个问题。不过,请务必发布您的代码 - 并说明徽标目前出现在哪里?右对齐,居中?
  • 这确实应该是一个新问题,但是this example 让它看起来像你想要im.hAlign = 'LEFT'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多