Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python:在reportlab的顶部添加徽标_Python 2.7_Reportlab - Fatal编程技术网

Python 2.7 Python:在reportlab的顶部添加徽标

Python 2.7 Python:在reportlab的顶部添加徽标,python-2.7,reportlab,Python 2.7,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

我正在使用
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)

您需要将徽标附加到
文档
,您将从中
生成()

添加
文档。在
im=图像(徽标,1*英寸,1*英寸)之后追加(im)


有一个很好的教程

@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)
之后追加(t)
?@Mawg谢谢你的帮助。但它不起作用。@Mawg我真的很抱歉。它起作用了。谢谢。请告诉我如何将徽标设置为左对齐?这是一个新问题,马丁。我们喜欢只回答一个问题和一个答案,否则会变得一团糟。发布一个新问题,但如果你愿意,可以链接到此问题。不过,一定要把你的代码贴出来,并告诉我们现在的标志出现在哪里?右对齐,居中?这确实应该是一个新问题,但会让它看起来像你想要的
im.hAlign='LEFT'