Python 将图像插入到从数据库查询的表中
我正在尝试创建一个包含带有Reportlab的表的报表。这些信息是从MySQL表中查询出来的,该表还保存了我想插入到pdf表中的图像的url 我可以轻松地在pdf中创建包含所需信息的表格。但是,我在将图片插入pdf中与所查询的值相同的行时遇到问题。有什么建议吗Python 将图像插入到从数据库查询的表中,python,mysql,report,reportlab,Python,Mysql,Report,Reportlab,我正在尝试创建一个包含带有Reportlab的表的报表。这些信息是从MySQL表中查询出来的,该表还保存了我想插入到pdf表中的图像的url 我可以轻松地在pdf中创建包含所需信息的表格。但是,我在将图片插入pdf中与所查询的值相同的行时遇到问题。有什么建议吗 from reportlab.lib import colors from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table from reportl
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()
如果您已经有了URL,为什么需要插入图片?我希望该图片包含在报告的表格中,如Id、FirstName、LastName、。我所说的报告是指PDFso,为什么你不编辑你的表,以明确你在数据库中谈论的表以及你在报告中谈论的表。e4c5我再次阅读了我的帖子,可以理解你的意思。我添加了更多信息来区分pdf和数据库中的表。为什么要在循环中创建表?您不应该只构建列表,然后将其传递给循环外的表吗