【问题标题】:Need two different aligns in one line using reportlab需要使用reportlab在一行中进行两个不同的对齐
【发布时间】:2021-02-27 00:57:13
【问题描述】:

我正在创建一个面向表格的 PDF,并让每一页的第一行重复。我希望这一行有一个居中的标题字符串,然后是一个与日期右对齐的字符串(都在同一行,以节省垂直空间)。我认为这会非常普遍,但我在任何地方都找不到这样的例子。在最简单的概念中,我背靠背使用了两个 para 标签,reportlab 最终只使用最后给出的对齐方式。然后,此标签字符串将在段落中使用,然后进入表格单元格。所以,我的问题是如何在reportlab 的一行中实现这两种对齐方案?或者,我可以交替使用两个段落背靠背(带有自定义样式)吗?

title_pdf = "<para align=center fontSize=14>{}</para><para align=right fontSize=11>{}</para>".format("PAGE TITLE","01/01/2021")
row_ary[0] = [Paragraph(title_pdf,style=styles["Normal"])]

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    如果您的表格可以与页眉分开,我将使用页面模板类来解决它,该类包含一个名为 onPage (BaseDocTemplate) 和 onFirstPage,onLaterPages (SimpleDocTemplate) 的属性。其中一些在用户指南 3.5.36 版的第 70 页上有所提及

    让它工作可能有点棘手,所以我在下面包含一些代码作为开始。

    from reportlab.lib.units import cm
    from reportlab.lib.pagesizes import A4, landscape
    from reportlab.platypus import Table, TableStyle
    from reportlab.platypus import SimpleDocTemplate
    from reportlab.lib import colors
    
    def func(canvas, doc):
        canvas.saveState()
    
        canvas.setFont("Helvetica", 12)
        (width, height) = landscape(A4)
        canvas.drawCentredString(width / 2.0, height - 1.2 * cm, "PAGE TITLE")
        canvas.drawRightString(width - 1 * cm, height - 1.2 * cm, "01/01/2021")
    
        canvas.restoreState()
        return func
    
    def create_pdf():
        story = []
        data = [['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8', 'Data9', 'Data10'],
                ['0.2', '-0.1', '0', '0', '-0.5', '0.6','0.2', '-0.1', '0', '0']]*200
    
        colwidths = (70)
        rowheights = (12)
    
        t = Table(data, colwidths, rowheights)
    
        GRID_STYLE = TableStyle(
            [('FONTSIZE', (0, 0), (-1, -1), 5),
             ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
             ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
             ('LEFTPADDING', (0, 0), (-1, -1), 0),
             ('RIGHTPADDING', (0, 0), (-1, -1), 0),
             ('TOPPADDING', (0, 0), (-1, -1), 0),
             ('BOTTOMPADDING', (0, 0), (-1, -1), 2),
             ('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
             ('SIZE', (0, 0), (-1, -1), 8),
             ('LEADING', (0, 0), (-1, -1), 8.2),
             ]
        )
    
        t.setStyle(GRID_STYLE)
        story.append(t)
    
        doc = SimpleDocTemplate('mydoc.pdf', pagesize=landscape(A4), topMargin=50)
        doc.build(story, onFirstPage=func, onLaterPages=func)
    
        # ----------------------------------------------------------------------
    if __name__ == "__main__":
        create_pdf()  # Printing the pdf
    

    【讨论】:

    • 我运行了你的代码,但它不太符合要求,但希望你能解决。例如,我正在寻找的是一个居中的“PAGE TITLE”,以及向右对齐的日期字符串——全部在一行上。当然,它可以像您所做的那样位于页眉中。你能挥动那个吗?
    • 看看修改后的版本,会不会更合适?
    【解决方案2】:

    您可以尝试使用三列表并让第一列为空。用TableStyle设置样式:

    from reportlab.platypus import Table, TableStyle
    from reportlab.platypus.paragraph import Paragraph
    
    title = "PAGE TITLE"
    date = "01/01/2021"
    
    table = Table([("", Paragraph(f"{title}"), Paragraph(f"{date}"))])
    style = TableStyle([("ALIGN", (1, 0), (1, 0), "CENTER"),  # Select second table col
                        ("ALIGN", (-1, -1), (-1, -1), "RIGHT")])  # Select last table col
    
    table.setStyle(style)
    

    【讨论】:

      猜你喜欢
      • 2022-12-19
      • 2012-11-05
      • 2014-04-23
      • 1970-01-01
      • 2018-06-24
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      相关资源
      最近更新 更多