首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >报告实验室不同的下一页

报告实验室不同的下一页
EN

Stack Overflow用户
提问于 2012-06-15 06:40:58
回答 1查看 4K关注 0票数 5

我正在尝试使用Python2.7和ReportLab生成一个具有不同奇偶页面布局的PDF文档(以允许非对称边框进行绑定)。为了使问题更加复杂,我正在尝试每页生成两列。

代码语言:javascript
运行
复制
def WritePDF(files):

    story = []
    doc = BaseDocTemplate("Polar.pdf", pagesize=A4, title = "Polar Document 5th Edition")

    oddf1  = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol1') 
    oddf2  = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol2')
    evenf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol1') 
    evenf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol2')
    doc.addPageTemplates([PageTemplate(id='EvenTwoC',frames=[evenf1,evenf2],onPage=evenFooter),
                          PageTemplate(id='OddTwoC', frames=[oddf1, oddf2], onPage=oddFooter)])


    ...

    story.append(Paragraph(whatever, style))

我不明白的是如何让ReportLab在左右(或者奇数和偶数)页面之间切换。有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-19 00:43:33

我想我找到了解决方案!:)

我不得不深入研究源代码。我在文件reportlab/platypus/doctemplate.py的636行找到了解决方案。这不是我第一次这样做了,因为文档非常有限……

现在,我发现了:

代码语言:javascript
运行
复制
def handle_nextPageTemplate(self,pt):
        '''On endPage change to the page template with name or index pt'''
        if type(pt) is StringType:
            # ... in short, set self._nextPageTemplate
        elif type(pt) is IntType:
            # ... in short, set self._nextPageTemplate
        elif type(pt) in (ListType, TupleType):
            #used for alternating left/right pages
            #collect the refs to the template objects, complain if any are bad
            c = PTCycle()
            for ptn in pt:
                found = 0
                if ptn=='*':    #special case name used to short circuit the iteration
                    c._restart = len(c)
                    continue
                for t in self.pageTemplates:
                    if t.id == ptn:
                        c.append(t)
                        found = 1
                if not found:
                    raise ValueError("Cannot find page template called %s" % ptn)
            if not c:
                raise ValueError("No valid page templates in cycle")
            elif c._restart>len(c):
                raise ValueError("Invalid cycle restart position")

            #ensure we start on the first one
            self._nextPageTemplateCycle = c.cyclicIterator()
        else:
            raise TypeError("argument pt should be string or integer or list")

我检查了这个self._nextPageTemplateCycle的使用位置,所以这是我认为应该可以工作的(虽然没有测试):

代码语言:javascript
运行
复制
story = []
# ...
# doc.addPageTemplates([...])

story.append(NextPageTemplate(['pageLeft', 'pageRight'])) # this will cycle through left/right/left/right/...

story.append(NextPageTemplate(['firstPage', 'secondPage', '*', 'pageLeft', 'pageRight'])) # this will cycle through first/second/left/right/left/right/...

当您想要开始交替页面时,只需将此内容添加到故事中一次。使用另一个普通的NextPageTemplate来停止这个循环(因为在源代码中,如果您这样做,就会有一个del self._nextPageTemplateCycle )。

希望它能帮上忙,如果它能工作,我现在还不能确定,但我会的!

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11042327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档