亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

reportlab SimpleDocTemplate - 使用可變行數設置表格的固定高度

reportlab SimpleDocTemplate - 使用可變行數設置表格的固定高度

慕娘9325324 2021-11-16 15:21:24
我嘗試Python使用reportlab.發票只有一頁,而且單頁上的空間永遠不會有更多的細節;我的代碼在生成 PDF 之前檢查最大詳細信息數?,F在我正在使用SimpleDocTemplate將所有內容添加到頁面,并使用Table. 這是一個簡化的代碼示例:from reportlab.lib.units import mmfrom reportlab.platypus import Paragraph, Spacer, Table, TableStylefrom reportlab.platypus import SimpleDocTemplate# add invoice headerflowable_list = [    Spacer(1, 5*mm),    Paragraph('Date: ...', pg_style_1),    Spacer(1, 5*mm),]# add invoice detailsdetail_list = [    ('Item 1', 8, 45),    ('Item 2', 1, 14),]row_list = [    [        Paragraph(desc, pg_style_1),        quantity,        amount,    ]    for desc, quantity, amount in detail_list]story.append(    Table(        data=row_list,        colWidths=[100*mm, 40*mm, 40*mm],        style=TableStyle([            ('VALIGN', (0, 0), (-1, -1), 'TOP'),            ... some other options ...        ])))# add invoice footer; this should be at a specific position on the pageflowable_list.append(Spacer(1, 5*mm))flowable_list.append(Paragraph('Total: 0', pg_style_1))# build PDFbuffer = io.BytesIO()doc = SimpleDocTemplate(buffer)doc.build(flowable_list)我的問題:底部的總金額每次都必須位于特定位置(類似于x*mm底部),但可能有可變數量的詳細信息,導致詳細信息表具有非固定高度。我目前的解決方案:Spacer在表后添加一個;這個間隔的高度必須根據表格中的行數來計算(更多的行意味著間隔會更??;更少的行產生更大的間隔)。但是,如果其中一行環繞并占用比單行更多的空間,則此操作將失敗。我的問題:有沒有辦法為詳細信息表設置固定高度,無論有多少行,但仍然繼續使用SimpleDocTemplate?這個類似的問題顯示了一個將所有內容手動繪制到畫布上的解決方案,但SimpleDocTemplate如果可能的話,我想要一種繼續使用 的方法。
查看完整描述

2 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

試試這個使用“TopPadder”的可行示例,(結果是 Total 被推到 Frame 的底部,我假設你可以在它下面添加一個墊子,以控制底部的高度):


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

from reportlab.lib.units import mm

from reportlab.platypus import Paragraph, Spacer, Table, TableStyle

from reportlab.lib.styles import getSampleStyleSheet

from reportlab.platypus import SimpleDocTemplate

from reportlab.platypus.flowables import TopPadder

import io


def create_pdf():


    styles=getSampleStyleSheet()


    # add invoice header

    flowable_list = [

        Spacer(1, 5 * mm),

        Paragraph(f'Date: ...', styles['Normal']),

        Spacer(1, 5 * mm),

    ]


    # add invoice details

    detail_list = [

        ('Item 1', 8, 45),

        ('Item 2', 1, 14),

    ]

    row_list = [

        [

            Paragraph(f'desc', styles['Normal']),

            # quantity,

            # amount,

        ]

        for desc, quantity, amount in detail_list]


    story = []

    story.append(

        Table(

            data=row_list,

            colWidths=[100 * mm, 40 * mm, 40 * mm],

            style=TableStyle([

                ('VALIGN', (0, 0), (-1, -1), 'TOP'),

                # ... some other options...

            ])))


    # add invoice footer; this should be at a specific position on the page

    flowable_list.append(Spacer(1, 5 * mm))

    flowable_list.append(TopPadder(Paragraph(f'Total: 0', styles['Normal'])))


    # build PDF

    buffer = io.BytesIO()

    doc = SimpleDocTemplate("test2.pdf")

    doc.build(flowable_list)


    # ----------------------------------------------------------------------

if __name__ == "__main__":

    create_pdf()  # Printing the pdf


查看完整回答
反對 回復 2021-11-16
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

我還沒有找到更好的方法,所以我將添加我當前的解決方案;也許它會幫助一些未來有類似問題的讀者。


...

story.append(

    Table(

        data=row_list,

        colWidths=[100*mm, 40*mm, 40*mm],

        style=TableStyle([

            ('VALIGN', (0, 0), (-1, -1), 'TOP'),

            ... some other options ...

        ])))


# calculate real height of details table, so we can add a 'Spacer' for the missing

# part to have the invoice totals at the correct height at the bottom of the page

_, real_height = story[-1].wrap(doc_width, doc_height)

height_reserved_for_details = 100*mm

remaining_height = max(0, height_reserved_for_details - real_height)

story.append(Spacer(1, remaining_height))


flowable_list.append(Paragraph('Total: 0', pg_style_1))


查看完整回答
反對 回復 2021-11-16
  • 2 回答
  • 0 關注
  • 496 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號