【问题标题】:Reportlab, simple issue with adding image to a tableReportlab,将图像添加到表格的简单问题
【发布时间】:2019-11-21 07:57:41
【问题描述】:

所以我正在创建这个简单的 Reportlab pdf 报告,但我在尝试在表格中显示图像时遇到问题,它显示 ((()))而不是图片

我将在此处发布我的整个代码,以便任何寻找如何创建 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'))

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    好吧,我想通了, 我没有使用 Image.open,而是使用了这个 image = Paragraph('''''', styleSheet["BodyText"]) 然后我将可变图像添加到表格中,它工作得很好:)

    【讨论】:

      【解决方案2】:

      尝试使用 drawImage() 属性

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

      【讨论】:

      • 是的,它是这样工作的,但这不是我想要的。我正在尝试将其添加到表格中并使其显示在表格的特定单元格中。你可以在我的代码中看到我实际上注释掉了 canvas.drawimage(..)
      猜你喜欢
      • 2021-01-24
      • 2011-07-09
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 2018-12-26
      • 2012-08-02
      相关资源
      最近更新 更多