python-2.7 - Python:在reportlab的顶部添加徽标
问题描述
我正在使用reportlab
inpython-2.7
生成 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()
。
document.append(im)
之后添加im = Image(logo, 1 * inch, 1 * inch)
。
这里有一个很好的教程
推荐阅读
- android - onclick imageview 时,Fragment Activity 崩溃
- powershell - Set-SPSiteAdministration cmdlet - 所有者用户别名
- path - 如何列出文件夹并返回特定文件类型的所有文件名?
- c# - 使用 ASP.NET MVC 和 IIS 10 浏览网站时出现错误 404
- json - JSON API - DirectException("请求正文不是 unjson()-able: %s" % body)
- google-cloud-platform - 从谷歌云存储库克隆项目作为成员
- c# - Sql server 过程选择作为插入过程的警报
- xml - 如何使用 one2many 字段为 many2one 字段设置域
- angular - 有没有办法在 Angular PipeDate 上实现 i18n
- java - 为此程序强制转换 IllegalArgumentException