我正在尝试创建一个包含Reportlab表的报表。从MySQL表中查询信息,该表还保存了我要在pdf表格中插入的图像的URL。
我可以轻松地在pdf中创建所需信息的表格。但是,我确实有问题将图片插入pdf中与查询值相同的行中。对此有何建议?
from reportlab.lib import colors
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table
from reportlab.lib.styles import getSampleStyleSheet
import MySQLdb
doc = SimpleDocTemplate("Students.pdf")
elements = []
styleSheet = getSampleStyleSheet()
# Connect to db
conn = MySQLdb.connect(host="localhost", user="test", passwd="test", db="test")
cursor = conn.cursor()
cursor.execute('SELECT id, FirstName, LastName, image FROM students');
rows = cursor.fetchall()
data = []
data.append(['Id', 'FirstName', 'LastName, 'Image'])
# loop through database select and a to list
for row in rows:
data.append(row)
t=Table(data,style=[
('GRID',(0,0),(-1,-1),0.5,colors.black),
('ALIGN',(0,0),(-1,-1),'CENTER'),
])
elements.append(t)
# write the document to disk
doc.build(elements)
conn.close()