Python Reportlab:更改页边距后重建文档?

Python Reportlab:更改页边距后重建文档?,python,reportlab,Python,Reportlab,在我为朋友的企业编写的一个程序中,我使用reportlab模块为各种报告构建PDF文档。在大多数情况下,报告可以包含在一页上,但在某些罕见的情况下,它可能跨越两页。在这些罕见的情况下,我想做的是用较小的上下页边距重新格式化页面,看看是否可以将其放在单个页面上。如果没有,我将使用最小的边距,让它跨越两页 为了生成报告,我正在创建SimpleDocTemplate类的一个实例。在将我所有的flowable传递给build方法之后,我发现我可以查询page属性来查看它使用了多少页面。以下是我尝试的第一

在我为朋友的企业编写的一个程序中,我使用reportlab模块为各种报告构建PDF文档。在大多数情况下,报告可以包含在一页上,但在某些罕见的情况下,它可能跨越两页。在这些罕见的情况下,我想做的是用较小的上下页边距重新格式化页面,看看是否可以将其放在单个页面上。如果没有,我将使用最小的边距,让它跨越两页

为了生成报告,我正在创建
SimpleDocTemplate
类的一个实例。在将我所有的flowable传递给
build
方法之后,我发现我可以查询
page
属性来查看它使用了多少页面。以下是我尝试的第一件事:

parts = []
path = "/path/to/my/file.pdf"
# ... a bunch of code that appends various flowables to 'parts'
doc = SimpleDocTemplate(path, pagesize=landscape(letter))
doc.build(parts)
# Shrink the margins while it's more than one page and the margins are above zero
while doc.page > 1 and not any([doc.topMargin <= 0, doc.bottomMargin <= 0]):
    # Decrease the page margins and rebuild the page
    doc.topMargin -= inch * .125
    doc.bottomMargin -= inch * .125
    doc.build(parts)
# If the margins are nil and we're still at 2 or more pages, use minimal margins
if doc.page > 1:
    doc.topMargin = inch * .25
    doc.bottomMargin = inch * .25
    doc.build(parts)
这导致了一些奇怪的异常,因此我尝试使用
copy
模块进行深度复制:

doc.build(copy.deepcopy(parts))
这没有抛出任何异常,但也没有改变利润率

有点绝望,我深入研究了
SimpleDocTemplate
属性,找到了一个名为
\u calc
的方法。考虑到这可能会重新计算页面,我尝试在更改页边距后调用它。它没有抛出任何异常,但也不起作用

我唯一成功的方法就是使用
deepcopy
流程,每次调整边距时都创建全新的文档:

doc = SimpleDocTemplate(path, pagesize=landscape(letter))
doc.build(copy.deepcopy(parts))
# Shrink the margins while it's more than one page and the margins are above zero
while doc.page > 1 and not any([doc.topMargin <= 0, doc.bottomMargin <= 0]):
    doc.topMargin -= inch * .125
    doc.bottomMargin -= inch * .125
    doc = SimpleDocTemplate(path, pagesize=landscape(letter),
                            topMargin = doc.topMargin,
                            bottomMargin = doc.bottomMargin)
    doc.build(copy.deepcopy(parts))
# If the margins are nil and we're still at 2 or more pages, use minimal margins
if doc.page > 1:
    doc.topMargin = inch * .25
    doc.bottomMargin = inch * .25
    doc = SimpleDocTemplate(path, pagesize=landscape(letter),
                            topMargin = doc.topMargin,
                            bottomMargin = doc.bottomMargin)
    doc.build(copy.deepcopy(parts))
doc=SimpleDocTemplate(路径,页面大小=横向(字母))
文件构建(副本深度副本(部分))
#当超过一页且页边距大于零时,缩小页边距

而doc.page>1而不是任何([doc.topMargin这是一个非常有趣的问题。但是,我相信您已经在ReportLab中找到了正确的方法。构建过程是您可以对文档执行的一次性操作,因为它会产生无法逆转的副作用。谢天谢地,正如您已经发现的那样,尽管有点烦人,但并不是h你想干什么就干什么

doc = SimpleDocTemplate(path, pagesize=landscape(letter))
doc.build(copy.deepcopy(parts))
# Shrink the margins while it's more than one page and the margins are above zero
while doc.page > 1 and not any([doc.topMargin <= 0, doc.bottomMargin <= 0]):
    doc.topMargin -= inch * .125
    doc.bottomMargin -= inch * .125
    doc = SimpleDocTemplate(path, pagesize=landscape(letter),
                            topMargin = doc.topMargin,
                            bottomMargin = doc.bottomMargin)
    doc.build(copy.deepcopy(parts))
# If the margins are nil and we're still at 2 or more pages, use minimal margins
if doc.page > 1:
    doc.topMargin = inch * .25
    doc.bottomMargin = inch * .25
    doc = SimpleDocTemplate(path, pagesize=landscape(letter),
                            topMargin = doc.topMargin,
                            bottomMargin = doc.bottomMargin)
    doc.build(copy.deepcopy(parts))