Reportlab:对齐、vAlign 表到帧的“底部" [英] Reportlab: Align, vAlign Table to the 'BOTTOM' of a Frame

查看:91
本文介绍了Reportlab:对齐、vAlign 表到帧的“底部"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 Table 对象与框架底部对齐时遇到问题,hAlign 'RIGHT' 和 'LEFT' 工作,但它似乎卡在 'TOP',我如何将 Table 向下对齐到 'MIDDLE'还是框架的底部"?下面是一个完整且可运行的示例.请注意,框架内的表格应该在底部,这意味着表格位于右下角(现在,下表位于框架的顶部).

 from reportlab.lib.pagesizes 导入信从 reportlab.lib 导入颜色从 reportlab.platypus 导入框架,PageTemplate从 reportlab.lib.units 导入厘米从 reportlab.platypus 导入(表、表样式、BaseDocTemplate)#####################################################################def create_pdf():"""创建一个pdf"""# 创建一个框架CatBox_frame = 框架(x1=14.00 * cm, # 从左y1=1.5 * cm, # 从底部高度=9.60 * 厘米,宽度=5.90 * 厘米,leftPadding=0 * 厘米,底部填充=0 *厘米,rightPadding=0 * 厘米,topPadding=0 * 厘米,显示边界=1,id='CatBox_frame')# 创建一个表猫盒 = 表([['', '', '', '一种'],['', '', '', 'B'],['', '', '', 'C'],['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')# 给表格添加样式CatBox.setStyle(TableStyle([('大小', (0, 0), (-1, -1), 7),('大小', (0, 0), (0, 0), 5.5),('TEXTCOLOR', (0, 0), (-1, -1), color.black),('GRID', (0, 0), (-1, -1), 0.5, color.black),('VALIGN', (0, 0), (-1, -1), '底部'),]))# 试图告诉 table 是一个底部对齐对象(稍后放入框架时)CatBox.Align = '底部'CatBox.vAlign = '底部'# 构建故事story = [CatBox] # 添加 CatBox 表(替代,story.add(CatBox))# 建立文件doc = BaseDocTemplate("BottomAlignTable.pdf", pagesize=letter)# 创建页面模板frontpage = PageTemplate(id='FrontPage',帧=[CatBox_frame])# 将故事添加到模板中,将模板添加到文档中doc.addPageTemplates(frontpage)# 构建文档doc.build(故事)# ----------------------------------------------------------------------如果 __name__ == "__main__":create_pdf() # 打印pdf

更新:我在 flowables.py 中发现了一个叫做 TopPadder 的东西,但不知道如何使用它(当 Align 'BOTTOM' 应该是右、左、顶部和中间).(如本文档第 4 页所示:

解决方案

也许应该是这样的:

doc = SimpleDocTemplate('CatBox.pdf')猫盒 = []表 = 表([['', '', '', '一种'],['', '', '', 'B'],['', '', '', 'C'],['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')table.setStyle(TableStyle([('大小', (0, 0), (-1, -1), 7),('大小', (0, 0), (0, 0), 5.5),('TEXTCOLOR', (0, 0), (-1, -1), color.black),('GRID', (0, 0), (-1, -1), 0.5, color.black),('VALIGN', (0, 0), (-1, -1), '底部'),]))table.Align = '底部'table.vAlign = '底部'table_frame = 框架(x1=7.0 * cm, # 从左y1=1.5 * cm, # 从底部高度=9.60 * 厘米,宽度=5.90 * 厘米,leftPadding=0 * 厘米,底部填充=0 *厘米,rightPadding=0 * 厘米,topPadding=0 * 厘米,显示边界=1,id='CatBox_frame')CatBox.append(table)模板 = PageTemplate(id='all', frames=table_frame)doc.addPageTemplates([模板])doc.build(CatBox)

基于 http://www.blog.pythonlibrary.org/2014/03/10/reportlab-how-to-create-custom-flowables/ 可能是:

doc = SimpleDocTemplate('CatBox.pdf')doc.build([CatBox(x=5, y=25)])类 CatBox(Flowable):def __init__(self, x=0, y=0):Flowable.__init__(self)自我.x = x自我.y = y#----------------------------------------------------------------------def坐标(自我,x,y,单位= 1):"""http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab帮助在 Canvas 对象中定位流动对象的助手类"""x, y = x * 单位, self.height - y * 单位返回 x, y#----------------------------------------------------------------------定义绘制(自我):"""绘制形状、文字等"""表 = 表([['', '', '', '一种'],['', '', '', 'B'],['', '', '', 'C'],['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')table.setStyle(TableStyle([('大小', (0, 0), (-1, -1), 7),('大小', (0, 0), (0, 0), 5.5),('TEXTCOLOR', (0, 0), (-1, -1), color.black),('GRID', (0, 0), (-1, -1), 0.5, color.black),('VALIGN', (0, 0), (-1, -1), '底部'),]))table.Align = '底部'table.vAlign = '底部'table.wrapOn(self.canv, self.width, self.height)table.drawOn(self.canv, *self.coord(self.x, self.y, cm))

I have problem to align a Table object to the bottom of a Frame, hAlign 'RIGHT' and 'LEFT' works, but it seem to be stuck in 'TOP', how do I vAlign the Table down to 'MIDDLE' or the 'BOTTOM' of the Frame? Below is a complete and run-able example. Please note it is table within the Frame that should be on the bottom, meaning the table is in the bottom right corner (now, below table is on TOP of the frame).

from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import Frame, PageTemplate
from reportlab.lib.units import cm
from reportlab.platypus import (Table, TableStyle, BaseDocTemplate)

########################################################################

def create_pdf():
    """
    Create a pdf
    """

    # Create a frame
    CatBox_frame = Frame(
        x1=14.00 * cm,  # From left
        y1=1.5 * cm,  # From bottom
        height=9.60 * cm,
        width=5.90 * cm,
        leftPadding=0 * cm,
        bottomPadding=0 * cm,
        rightPadding=0 * cm,
        topPadding=0 * cm,
        showBoundary=1,
        id='CatBox_frame')

    # Create a table
    CatBox = Table([
        ['', '', '', 'A'],
        ['', '', '', 'B'],
        ['', '', '', 'C'],
        ['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')

    # Style the table
    CatBox.setStyle(TableStyle([
        ('SIZE', (0, 0), (-1, -1), 7),
        ('SIZE', (0, 0), (0, 0), 5.5),
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
        ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
        ('VALIGN', (0, 0), (-1, -1), 'BOTTOM'),
    ]))

    # Trying to tell the table to be a bottom align object (when later put in frame)
    CatBox.Align = 'BOTTOM'
    CatBox.vAlign = 'BOTTOM'

    # Building the story
    story = [CatBox] # adding CatBox table (alternative, story.add(CatBox))

    # Establish a document
    doc = BaseDocTemplate("BottomAlignTable.pdf", pagesize=letter)

    # Creating a page template 
    frontpage = PageTemplate(id='FrontPage',
                             frames=[CatBox_frame]
                             )
    # Adding the story to the template and template to the document
    doc.addPageTemplates(frontpage)

    # Building doc
    doc.build(story)


# ----------------------------------------------------------------------
if __name__ == "__main__":
    create_pdf() # Printing the pdf

UPDATE: I found something called TopPadder in flowables.py, but yet no clue howto use it (and would feel like a hack/ a weird application when the Align 'BOTTOM' ought to be the logical completion of right, left, top and middle). (as illustrated on page 4 in this document: https://www.reportlab.com/examples/rml/test/test_008_tables.pdf )

class TopPadder(Flowable): '''wrap a single flowable so that its first bit will be padded to fill out the space so that it appears at the bottom of its frame'''

UPDATE II: Yes, I solved it, the solution was:

from reportlab.platypus.flowables import TopPadder

story = [TopPadder(CatBox)]

解决方案

Maybe should be like this:

doc = SimpleDocTemplate('CatBox.pdf')

CatBox = []
table = Table([
    ['', '', '', 'A'],
    ['', '', '', 'B'],
    ['', '', '', 'C'],
    ['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')

table.setStyle(TableStyle([
    ('SIZE', (0, 0), (-1, -1), 7),
    ('SIZE', (0, 0), (0, 0), 5.5),
    ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
    ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
    ('VALIGN', (0, 0), (-1, -1), 'BOTTOM'),
]))

table.Align = 'BOTTOM'
table.vAlign = 'BOTTOM'

table_frame = Frame(
    x1=7.0 * cm,  # From left
    y1=1.5 * cm,  # From bottom
    height=9.60 * cm,
    width=5.90 * cm,
    leftPadding=0 * cm,
    bottomPadding=0 * cm,
    rightPadding=0 * cm,
    topPadding=0 * cm,
    showBoundary=1,
    id='CatBox_frame'
)

CatBox.append(table)

template = PageTemplate(id='all', frames=table_frame)
doc.addPageTemplates([template])

doc.build(CatBox)

An alternative based on http://www.blog.pythonlibrary.org/2014/03/10/reportlab-how-to-create-custom-flowables/ could be:

doc = SimpleDocTemplate('CatBox.pdf')
doc.build([CatBox(x=5, y=25)])

class CatBox(Flowable):

    def __init__(self, x=0, y=0):
        Flowable.__init__(self)
        self.x = x
        self.y = y

    #----------------------------------------------------------------------
    def coord(self, x, y, unit=1):
        """
        http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab
        Helper class to help position flowables in Canvas objects
        """
        x, y = x * unit, self.height - y * unit
        return x, y

    #----------------------------------------------------------------------
    def draw(self):
        """
        Draw the shape, text, etc
        """

        table = Table([
            ['', '', '', 'A'],
            ['', '', '', 'B'],
            ['', '', '', 'C'],
            ['AA', 'BB', 'CC', '']], 1.2 * cm, 1.2 * cm, vAlign='BOTTOM')

        table.setStyle(TableStyle([
            ('SIZE', (0, 0), (-1, -1), 7),
            ('SIZE', (0, 0), (0, 0), 5.5),
            ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
            ('VALIGN', (0, 0), (-1, -1), 'BOTTOM'),
        ]))

        table.Align = 'BOTTOM'
        table.vAlign = 'BOTTOM'

        table.wrapOn(self.canv, self.width, self.height)
        table.drawOn(self.canv, *self.coord(self.x, self.y, cm))

这篇关于Reportlab:对齐、vAlign 表到帧的“底部"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆