Reportlab,将图像添加到 table 的简单问题

Reportlab, simple issue with adding image to a table

所以我正在创建这个简单的 Reportlab pdf 报告,但我在尝试在 table 中显示图像时遇到问题,它显示 ((< PIL.Image.Image image mode=P size=240x60在 0x201E9A99F60>))) 而不是图像

我将 post 我的全部代码放在这里,这样它可以让任何寻找如何创建 Reportlab pdf 文档的人受益。

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,KeepTogether,tables
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import A4,landscape
from reportlab.lib.units import inch,cm,mm
from reportlab.pdfgen import canvas
from reportlab.platypus import PageBreak 
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
from reportlab.lib.utils import haveImages, fileName2FSEnc
import datetime
sample_style_sheet = getSampleStyleSheet()
styleSheet = getSampleStyleSheet()
from PIL import Image
import boto3
from boto3.dynamodb.conditions import Key, Attr
from reportlab.lib.pagesizes import letter, landscape

PAGE_HEIGHT = defaultPageSize[1]
PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
style = styles["Normal"]

#canvas = canvas.Canvas("emp_tasks_info2.14.pdf", pagesize=landscape(letter))

canvas = canvas.Canvas("emp_tasks_info2.14.pdf", pagesize=(1000,540))

canvas.saveState()
canvas.setFont('Times-Bold',15)

canvas.setFont('Helvetica', 11)
#canvas.line(480,747,580,747)
#canvas.drawInlineImage("C:\Users\youse\OneDrive\Pictures\1.jpeg", inch*.25, inch*.25, PAGE_WIDTH-(.5*inch), (.316*inch))
from reportlab.lib.testutils import testsFolder
#gif = os.path.join(testsFolder,'C:/Users/youse/OneDrive/Pictures/1.gif')

#canvas.drawImage( 'giphy.gif', 10,20, width=None,height=None,mask=None)
#canvas.drawImage( 'giphy.gif', 20,500, width=200,height=80,mask=None)
#doc.afterPage()
#canvas.restoreState()

#canvas.saveState()
#canvas.restoreState()
I = Image.open('giphy.gif')
I = I.resize((240,60))

P0 = Paragraph('''<b>A pa<font color=red>r</font>a<i>graph</i></b><super><font color=yellow>1</font></super>''',styleSheet["BodyText"]) 

pp = Paragraph('''<para align=center><b>Calls</b></para>''', styleSheet["BodyText"]) 
p0 = Paragraph('''<para align=center><b>1. Worked on Product-1439</b></para>''', styleSheet["BodyText"]) 
P = Paragraph('''<para align=center spaceb=3><b>Completed below Products for Incorporated:<br/>1. Product-14332.<br/>2. Product-14343. <br/>3. Product-14354. <br/>4.Product-14385.<br/>5. Product-1437 - LS-2574</b></para>''', styleSheet["BodyText"]) 
P1 = Paragraph('''<para align=right><b>Date:</b></para>''', styleSheet["BodyText"]) 
p2 = Paragraph('''<para align=center><b>Regression on completed projects</b></para>''', styleSheet["BodyText"]) 
p3 = Paragraph('''<para align=center><b>Performed Deployment testing and started with Test Plan for Sprint 18</b></para>''', styleSheet["BodyText"]) 
p4 = Paragraph('''<para align=center><b>Worked on Test plan for PRODUCT-1489</b></para>''', styleSheet["BodyText"]) 
p5 = Paragraph('''<para align=center><b>Total<br/>Week<br/>Regular</b></para>''', styleSheet["BodyText"])
p6 = Paragraph('''<para align=left ><b>Week Ending:</b></para>''', styleSheet["BodyText"])
jira = Paragraph('''<para align=center ><b>Jira Task</b></para>''', styleSheet["BodyText"])
sat = Paragraph('''<para align=center ><b>Sat</b></para>''', styleSheet["BodyText"])
sun = Paragraph('''<para align=center ><b>Sun</b></para>''', styleSheet["BodyText"])
mon = Paragraph('''<para align=center><b>Mon</b></para>''', styleSheet["BodyText"])
#mon = Paragraph('<p><b>Mon</b></p>', styleSheet["BodyText"])
tue = Paragraph('''<para align=center ><b>Tue</b></para>''', styleSheet["BodyText"])
wed = Paragraph('''<para align=center ><b>Wed</b></para>''', styleSheet["BodyText"])
thu = Paragraph('''<para align=center ><b>Thu</b></para>''', styleSheet["BodyText"])
fri = Paragraph('''<para align=center ><b>Fri</b></para>''', styleSheet["BodyText"])
employee = Paragraph('''<para align=right ><b>Employee Name:</b></para>''', styleSheet["BodyText"])
name = Paragraph('''<para align=left >Anjpp jol</para>''', styleSheet["BodyText"])
date = Paragraph('''<para align=center ><b>October 10, 2019</b></para>''', styleSheet["BodyText"])

data= [[I, '', '',  '', '','','','',''],
       [p6, employee, '',  '', name,'','','',''],
       [date, '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',p5],
       [P1,   '9/28', '9/29',  '9/30', '10/1', '10/2', '10/3','10/4', ''],        
       [jira, sat, sun, mon, tue, wed, thu, fri],
       [pp, '', '',  '', '','','','1.00',''],
       [p0, '', '', '8.00', '','','','','8.00'],        
       [P, '', '',  '', '8.00','','','','8.00'],        
       [p2, '', '',  '', '', '8.00', '','','8.00'],
       [p3, '', '',  '', '','','8.00','','8.00'],
       [p4, '', '',  '', '','','','7.00','8.00'],
       ['', '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',''],
       [I, '', '',  '', '','','','',''],
       ['', '', '',  '', '','','','',''],
       ['', '', '',  '8.00', '8.00','8.00','8.00','8.00','40.00']]
t=Table(data,style=[('GRID',(0,1),(-1,1),1,colors.white),
                    ('GRID',(0,2),(0,2),1,colors.black),
                    #('GRID',(1,3),(-1,-2),1,colors.black),
                    ('GRID',(0,6),(-1,-2),1,colors.black),
                    #('GRID',(1,-1),(-1,-1),1,colors.black),
                    ('GRID',(1,-1),(-1,-1),2,colors.black),
                    ('GRID',(0,6),(0,6),2,colors.black),
                    ('GRID',(1,5),(-1,6),2,colors.black),
                    #('BOX',(0,1),(-1,-1),1,colors.black),
                    ('BOX',(1,5),(-1,-1),2,colors.black),
                    ('BOX',(-1,4),(-1,-1),2,colors.black),
                    ('LINEABOVE',(0,-1),(-1,-1),2,colors.black),
                    ('LINEABOVE',(4,2),(-1,2),1,colors.black),
                    ('LINEABOVE',(4,3),(-1,3),1,colors.black),
                    #('LINEBEFORE',(0,4),(0,4),1,colors.white),                    
                    ('BACKGROUND', (0, 6), (0, 6), colors.skyblue),                    
                    ('BACKGROUND', (1, 5), (-1, 6), colors.skyblue),
                    ('BACKGROUND', (-1, 4), (-1, 4), colors.skyblue),
                    ('BACKGROUND', (0, 2), (0, 2), colors.skyblue),
                    #('VALIGN',(3,0),(3,0),'BOTTOM'),                    
                    #('BACKGROUND',(3,0),(3,0),colors.limegreen),                    
                    #('BACKGROUND',(3,1),(3,1),colors.khaki),                    
                    #('ALIGN',(3,1),(3,1),'CENTER'),                    
                    #('BACKGROUND',(3,2),(3,2),colors.beige),
                    ('VALIGN',(0,1),(-1,-1),'MIDDLE'),
                    ('ALIGN',(0,1),(-1,-1),'CENTER'),
                    #('VALIGN',(1,0),(-1,0),'BOTTOM'),
                    #('ALIGN',(1,0),(3,0),'RIGHT'),  
                    ('SPAN',(-1,4),(-1,6)),
                    ('SPAN',(1,1),(3,1)),
                    ('SPAN',(4,1),(-1,1))])

t._argW[0]=6.5*inch
t._argW[8]=.8*inch
t._argW[1]=.6*inch
t._argW[2]=.6*inch
t._argW[3]=.6*inch
t._argW[4]=.6*inch
t._argW[5]=.6*inch
t._argW[6]=.6*inch
t._argW[7]=.6*inch
t._argH[5]=.2*inch
t._argH[4]=.2*inch
t._argH[3]=.2*inch
t._argH[2]=.2*inch
t._argH[1]=.2*inch

t.wrapOn(canvas, 800, 760)
t.drawOn(canvas, 80, 100)
##########
#canvas.showPage()
#P0 = Paragraph('''#<b>A pa<font color=red>r</font>a<i>graph</i></b><super><font color=yellow>1</font></super>''',styleSheet["BodyText"])


canvas.save()


#Story.append(canvas.drawString(50,453,'Patient Lifestyle'))

尝试使用 drawImage() 属性

canvas.drawImage('giphy.gif', coordX, coordY, width=240, height=60)

好的,我明白了, 我没有使用 Image.open,而是使用了这个 image = Paragraph('''''', styleSheet["BodyText"]) 然后我将可变图像添加到 table 并且它工作得很好 :)