2016-08-10 19 views
1

reportlabを使用して1回のプログラム実行で複数のPDFドキュメントを作成するテンプレート関数を使用しています。目次を含む複数のドキュメントを作成するときにReportlabでシーケンスがリセットされない

これらのドキュメントは構造が同一であり、同じ見出しがあります。見出しの下の内容だけが異なります。これらの文書はすべて目次要素を含んでいます。

私は、番号付き見出しeを作成するためにシーケンスタグ(<seq/>など)を使用しています。 g。

1. Top1 
    1.1 Sub1 
2. Top2 
    2.1 Sub1 
    2.2 Sub2 

これは、1つの文書に適していますが、できるだけ早く私は2番目に作成するように右最初の後にシーケンスがリセットされておらず、第2の文書のTOCは

第三文書を作成
2. Top1 
    2.1 Sub1 
3. Top2 
    3.1 Sub1 
    3.2 Sub2 

のように見えますTop13で始まります。

新しい文書を作成しているので、新しいBaseDocTemplateクラスを作成しているので、シーケンスをリセットすることを期待していました。それをどうすれば実現できますか?

私は、reportlabのチュートリアルの1つを使用してできるだけ小さな例を作成しようとしました。

from reportlab.lib.styles import ParagraphStyle as PS 
from reportlab.platypus import PageBreak 
from reportlab.platypus.paragraph import Paragraph 
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate 
from reportlab.platypus.tableofcontents import TableOfContents 
from reportlab.platypus.frames import Frame 
from reportlab.lib.units import cm 

class MyDocTemplate(BaseDocTemplate): 
    def __init__(self, filename, **kw): 
     self.allowSplitting = 0 
     super().__init__(filename, **kw) 
     template = PageTemplate('normal', [Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')]) 
     self.addPageTemplates(template) 

# Entries to the table of contents can be done either manually by 
# calling the addEntry method on the TableOfContents object or automatically 
# by sending a 'TOCEntry' notification in the afterFlowable method of 
# the DocTemplate you are using. The data to be passed to notify is a list 
# of three or four items countaining a level number, the entry text, the page 
# number and an optional destination key which the entry should point to. 
# This list will usually be created in a document template's method like 
# afterFlowable(), making notification calls using the notify() method 
# with appropriate data. 

    def afterFlowable(self, flowable): 
     "Registers TOC entries." 
     if flowable.__class__.__name__ == 'Paragraph': 
      text = flowable.getPlainText() 
      style = flowable.style.name 
      if style == 'Heading1': 
       self.notify('TOCEntry', (0, text, self.page)) 
      if style == 'Heading2': 
       self.notify('TOCEntry', (1, text, self.page)) 


centered = PS(name = 'centered', 
    fontSize = 30, 
    leading = 16, 
    alignment = 1, 
    spaceAfter = 20) 

h1 = PS(
    name = 'Heading1', 
    fontSize = 14, 
    leading = 16) 


h2 = PS(name = 'Heading2', 
    fontSize = 12, 
    leading = 14) 

# Heading definition with sequence numbers 
heading = { 
    1 : "<seq id='h1'/>.<seqreset id='h2'/><seqreset id='h3'/> {}", 
    2 : "<seq id='h1' inc='no'/>.<seq id='h2'/><seqreset id='h3'/> {}", 
    3 : "<seq id='h1' inc='no'/>.<seq id='h2' inc='no'/>.<seq id='h3'/> {}", 
} 

def build_document(filename): 
    # Build story. 
    story = [] 

    # Create an instance of TableOfContents. Override the level styles (optional) 
    # and add the object to the story 

    toc = TableOfContents() 
    toc.levelStyles = [ 
     PS(fontName='Times-Bold', fontSize=20, name='TOCHeading1', leftIndent=20, firstLineIndent=-20, spaceBefore=10, leading=16), 
     PS(fontSize=18, name='TOCHeading2', leftIndent=40, firstLineIndent=-20, spaceBefore=5, leading=12), 
    ] 
    story.append(toc) 

    story.append(Paragraph('<b>Table of contents</b>', centered)) 
    story.append(PageBreak()) 
    story.append(Paragraph(heading[1].format('First heading'), h1)) 
    story.append(Paragraph('Text in first heading', PS('body'))) 
    story.append(Paragraph(heading[2].format('First sub heading'), h2)) 
    story.append(Paragraph('Text in first sub heading', PS('body'))) 
    story.append(PageBreak()) 
    story.append(Paragraph(heading[2].format('Second sub heading'), h2)) 
    story.append(Paragraph('Text in second sub heading', PS('body'))) 
    story.append(PageBreak()) 
    story.append(Paragraph(heading[2].format('Last heading'), h1)) 
    doc = MyDocTemplate(filename) 
    doc.multiBuild(story) 

if __name__ == "__main__": 
    build_document("1.pdf") 
    build_document("2.pdf") 

答えて

1

私は私の問題を解決しますが、私は最終的な解決策として、好きではないその迅速な解決策を発見しました。

問題は、同じ名前のグローバルシーケンスを使用していることです。 h1,h2およびh3がすべての文書に記載されています。そして、新しい文書が開始されたときに、reportlabはそれらをリセットしていないようです。代わりに、私はストーリーリストを書き込む前に手動でリセットします。

def build_document(filename): 
    # Build story. story = [] 

    # Reset the sequences 
    story.append(Paragraph("<seqreset id='h1'/>", PS('body'))) 
    story.append(Paragraph("<seqreset id='h2'/>", PS('body'))) 
    story.append(Paragraph("<seqreset id='h3'/>", PS('body'))) 

    # ... rest of the code from the question 
+0

最初の ''だけでは十分ではありませんか? 'h1'をはじめて使用すると他の設定は既にリセットされていますか? – B8vrede

+1

@ B8vrede no。それは十分ではないようです。私はそれをテストして、シーケンスはまだ増分されています。私はシーケンスを連鎖させることが可能であることを知っていますし、 'h1'を再設定するだけで十分ですが、私の例では連鎖しないようです。 – ap0

関連する問題