Python3: reportlab で自動で改ページをする


Ubuntu でライブラリーのインストール方法

sudo apt install python3-reportlab

次のような PDF を作成します。
テーブルが大きくなると改ページをします。

table_long.py
#! /usr/bin/python
#
#   table_long.py
#
#                      Feb/16/2019
# ------------------------------------------------------------------
import sys
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import mm
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.platypus import Paragraph
from reportlab.platypus import Spacer
# ------------------------------------------------------------------
# [8]:
def pdf_gen_proc(file_pdf,data):
    doc = SimpleDocTemplate(file_pdf,pagesize=A4)
    fontname_g = "HeiseiKakuGo-W5"
    pdfmetrics.registerFont(UnicodeCIDFont(fontname_g))
    elements = []
#
    style = ParagraphStyle(
        name='Normal',
        fontName='HeiseiKakuGo-W5',
        fontSize=20,
    )
#
    elements.append(Paragraph('テスト',style))
    elements.append(Spacer(1,10*mm))
#
    elements.append(Paragraph('Feb/16/2019',style))
    elements.append(Spacer(1,10*mm))
#
    elements.append(Paragraph('おはようございます。',style))
    elements.append(Spacer(1,10*mm))
#
    elements.append(Paragraph('Good Morning',style))
    elements.append(Spacer(1,10*mm))
#
    tt=Table(data)
    tt.setStyle(TableStyle([
        ('FONT',(0, 0),(-1, -1),"HeiseiKakuGo-W5",20),
        ('GRID',(0, 0),(len(data[0]),len(data)),0.25,colors.black),]))
    elements.append(tt)
#
    elements.append(Spacer(1,10*mm))
    elements.append(Paragraph('終了',style))
    elements.append(Spacer(1,10*mm))
#
    elements.append(Paragraph('The End',style))
    elements.append(Spacer(1,10*mm))
#
    doc.build(elements)
# ------------------------------------------------------------------
# [4]:
def data_gen_proc():
    data = []
#
    for it in range(10):
        jt = it * 10
        unit_a = [jt,"おはよう","aaa","bbb","ccc","ddd"]
        unit_b = [jt + 1,"こんにちは","春","夏","秋","冬"]
        unit_c = [jt + 2,"今晩は","朝","昼","夕","晩"]
        data.append(unit_a)
        data.append(unit_b)
        data.append(unit_c)
#
    return data
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
file_pdf = sys.argv[1]
#
data = data_gen_proc()
pdf_gen_proc(file_pdf,data)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------