【问题标题】:Python reportlab inserting image into tablePython reportlab将图像插入表格
【发布时间】:2013-07-28 18:16:31
【问题描述】:

在reportlab user guide(第79页)中,这是将图像插入表格的方式。

I = Image('../images/replogo.gif')
I.drawHeight = 1.25*inch*I.drawHeight / I.drawWidth
I.drawWidth = 1.25*inch
P0 = Paragraph('''
               <b>A pa<font color=red>r</font>a<i>graph</i></b>
               <super><font color=yellow>1</font></super>''',
               styleSheet["BodyText"])
P = Paragraph('''
       <para align=center spaceb=3>The <b>ReportLab Left
       <font color=red>Logo</font></b>
       Image</para>''',
       styleSheet["BodyText"])
data=  [['A',   'B', 'C',     P0, 'D'],
        ['00', '01', '02', [I,P], '04'],
        ['10', '11', '12', [P,I], '14'],
        ['20', '21', '22',  '23', '24'],
        ['30', '31', '32',  '33', '34']]
t=Table(data,style=[('GRID',(1,1),(-2,-2),1,colors.green),
                    ('BOX',(0,0),(1,-1),2,colors.red),
                    ('LINEABOVE',(1,2),(-2,2),1,colors.blue),
                    ('LINEBEFORE',(2,1),(2,-2),1,colors.pink),
                    ('BACKGROUND', (0, 0), (0, 1), colors.pink),
                    ('BACKGROUND', (1, 1), (1, 2), colors.lavender),
                    ('BACKGROUND', (2, 2), (2, 3), colors.orange),
                    ('BOX',(0,0),(-1,-1),2,colors.black),
                    ('GRID',(0,0),(-1,-1),0.5,colors.black),
                    ('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),
                    ('ALIGN',(3,2),(3,2),'LEFT'),
                    ])

所以根据上面的用户指南,我假设要将图像插入表格,我所要做的就是使用 PIL 导入图像,图像模块,加载图像并将其用作表格的数据。

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, cm,landscape
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER
from reportlab.lib import colors
from PIL import Image as im
a=im.open("temp.jpg")
data=[['1',a],['3','4']]
c = canvas.Canvas("Reportlabtest.pdf", pagesize=landscape(A4))
table = Table(data, colWidths=200, rowHeights=50)
table.setStyle(TableStyle([
                           ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                           ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                           ('BACKGROUND',(0,0),(-1,2),colors.lightgrey)
                           ]))
table.wrapOn(c, 200, 400)
table.drawOn(c,20,50)
c.save()

但我没有将图像插入表格,而是在 pdf "PIL.Jpegimageplugin.JPEGImageFile mode=RGB size=200x200 at 0x179B1E8" 上创建了这条消息

可能是什么问题?

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    您可以使用 ReportLab 的 Image 来代替 PIL.. 希望它对您有所帮助。我已将您的代码修改为以下内容。

    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import A4, cm,landscape
    from reportlab.lib.styles import getSampleStyleSheet
    from reportlab.platypus import Paragraph, Table, TableStyle, Image
    from reportlab.lib.enums import TA_LEFT, TA_CENTER
    from reportlab.lib import colors
    from reportlab.lib.units import inch
    
    a = Image.open("path/to/temp.jpg")  
    a.drawHeight = 2*inch
    a.drawWidth = 2*inch
    data=[['1',a],['3','4']]
    c = canvas.Canvas("Reportlabtest.pdf", pagesize=landscape(A4))
    table = Table(data, colWidths=200, rowHeights=50)
    table.setStyle(TableStyle([
                               ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                               ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                               ('BACKGROUND',(0,0),(-1,2),colors.lightgrey)
                               ]))
    table.wrapOn(c, 200, 400)
    table.drawOn(c,20,50)
    c.save()
    

    编辑:导入cm函数,我们应该使用:

    from reportlab.lib.pagesizes import A4, landscape
    from reportlab.lib.units import cm
    

    【讨论】:

    • 函数open()在2018年不再存在。你必须将参数发送给构造函数。 Image("path/to/temp.jpg", 2*inch, 2*inch)
    • 引发错误:ImportError: cannot import name 'cm' from 'reportlab.lib.pagesizes' (D:\anaconda\lib\site-packages\reportlab\lib\pagesizes.py).
    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2015-05-27
    • 2020-09-24
    • 1970-01-01
    • 2013-06-03
    • 2016-10-02
    • 2017-04-09
    • 2016-06-27
    相关资源
    最近更新 更多