【发布时间】: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