Python Reportlab中的动态framesize

2024-10-02 10:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python中的生成一个发货列表。 我试图通过使用PlatypusFrames将所有部分(如发送者地址、接收者地址、表)放置到位。在

我遇到的第一个问题是我需要大量的Frames来正确定位所有东西,有没有更好的方法使用鸭嘴兽? 因为我希望发件人地址和我的地址在同一高度上,如果我只将它们添加到我的story = []上,它们就会一个比一个对齐。在

下一个问题是我正在绘制的表的大小是动态的,当我到达Frame(我希望表去的空间)的末尾时,它只执行FrameBreak并在下一帧中连续。那么如何使Frame(我的表的空间)是动态的呢?在


Tags: 方法定位列表frames地址空间动态frame
1条回答
网友
1楼 · 发布于 2024-10-02 10:33:46

您的用例非常常见,因此Reportlab有一个系统可以帮助您解决问题。在

如果您阅读关于platypus的用户指南,它将向您介绍4个主要概念:

DocTemplates the outermost container for the document;

PageTemplates specifications for layouts of pages of various kinds;

Frames specifications of regions in pages that can contain flowing text or graphics.

Flowables Using PageTemplates you can combine "static" content with dynamic on a page in a sensible way like for example logo's, addresses and such.

你已经发现了FlowablesFrames,但很可能你还没有开始幻想PageTemplates或{}。这是有意义的,因为对于大多数简单的文档来说,这是不必要的。遗憾的是,发货单不是一个简单的文档,它包含地址、徽标和重要信息,这些信息必须出现在每一页上。这就是PageTemplates的作用。在

那么如何使用这些模板呢?这个概念很简单,每个页面都有一个特定的结构,在页面之间可能会有所不同,例如在第一页上,您希望输入地址,然后启动表,而在第二页,您只需要表。应该是这样的:

第1页:enter image description here

第2页:enter image description here

示例如下:

(如果有Reportlab的文档,这将非常适合于SO文档)

from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate, NextPageTemplate, Paragraph, PageBreak, Table, \
    TableStyle


class ShippingListReport(BaseDocTemplate):
    def __init__(self, filename, their_adress, objects, **kwargs):
        super().__init__(filename, page_size=A4, _pageBreakQuick=0, **kwargs)
        self.their_adress = their_adress
        self.objects = objects

        self.page_width = (self.width + self.leftMargin * 2)
        self.page_height = (self.height + self.bottomMargin * 2)


        styles = getSampleStyleSheet()

        # Setting up the frames, frames are use for dynamic content not fixed page elements
        first_page_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width, self.height - 6 * cm, id='small_table')
        later_pages_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='large_table')

        # Creating the page templates
        first_page = PageTemplate(id='FirstPage', frames=[first_page_table_frame], onPage=self.on_first_page)
        later_pages = PageTemplate(id='LaterPages', frames=[later_pages_table_frame], onPage=self.add_default_info)
        self.addPageTemplates([first_page, later_pages])

        # Tell Reportlab to use the other template on the later pages,
        # by the default the first template that was added is used for the first page.
        story = [NextPageTemplate(['*', 'LaterPages'])]

        table_grid = [["Product", "Quantity"]]
        # Add the objects
        for shipped_object in self.objects:
            table_grid.append([shipped_object, "42"])

        story.append(Table(table_grid, repeatRows=1, colWidths=[0.5 * self.width, 0.5 * self.width],
                           style=TableStyle([('GRID',(0,1),(-1,-1),0.25,colors.gray),
                                             ('BOX', (0,0), (-1,-1), 1.0, colors.black),
                                             ('BOX', (0,0), (1,0), 1.0, colors.black),
                                             ])))

        self.build(story)

    def on_first_page(self, canvas, doc):
        canvas.saveState()
        # Add the logo and other default stuff
        self.add_default_info(canvas, doc)

        canvas.drawString(doc.leftMargin, doc.height, "My address")
        canvas.drawString(0.5 * doc.page_width, doc.height, self.their_adress)

        canvas.restoreState()

    def add_default_info(self, canvas, doc):
        canvas.saveState()
        canvas.drawCentredString(0.5 * (doc.page_width), doc.page_height - 2.5 * cm, "Company Name")

        canvas.restoreState()


if __name__ == '__main__':
    ShippingListReport('example.pdf', "Their address", ["Product", "Product"] * 50)

相关问题 更多 >

    热门问题