【问题标题】:Reportlab: Align, vAlign Table to the 'BOTTOM' of a FrameReportlab:将表格对齐,vAlign 到框架的“底部”
【发布时间】:2019-02-23 00:11:41
【问题描述】:

我无法将表格对象对齐到框架的底部,hAlign 'RIGHT' 和 'LEFT' 有效,但它似乎卡在了 'TOP' 中,我如何将表格 vAlign 到 'MIDDLE'还是框架的“底部”?下面是一个完整且可运行的示例。请注意,框架内的表格应该在底部,这意味着表格在右下角(现在,下面的表格在框架的顶部)。

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

更新:我在 flowables.py 中找到了一个名为 TopPadder 的东西,但不知道如何使用它(当 Align 'BOTTOM' 应该是右、左、顶部和中间)。 (如本文档第 4 页所示:https://www.reportlab.com/examples/rml/test/test_008_tables.pdf

类 TopPadder(Flowable): '''包装一个单一的流动,以便它的第一位将是 填充以填充空间,使其出现在 框架的底部'''

更新二:是的,我解决了,解决方案是:

from reportlab.platypus.flowables import TopPadder

story = [TopPadder(CatBox)]

【问题讨论】:

  • 您能否将您的解决方案添加为该问题的实际答案? (感谢它刚刚帮助了我)

标签: python pdf reportlab


【解决方案1】:

也许应该是这样的:

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)

基于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)])

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))

【讨论】:

  • 您好,感谢您的回复。我运行了您的示例,但没有解决。我用一个可运行的示例更新了我的问题,以更清楚地说明。应该在矩形框架中向下移动的是表格(正方形 4x4)。我可以理解,我最终可以通过在画布上进行低级“破解”,但理想情况下,我想坚持 Platypus 中的功能。
【解决方案2】:

Top Padder 会解决的。

class TopPadder(Flowable): '''包装一个单独的 flowable 以便它的第一个 位将被填充以填充空间,使其出现在 框架的底部'''

在构建“故事”时添加以下内容。

from reportlab.platypus.flowables import TopPadder

story = [TopPadder(CatBox)]

【讨论】:

    猜你喜欢
    • 2011-01-02
    • 2011-12-30
    • 2015-02-28
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2017-07-23
    相关资源
    最近更新 更多