SPAN,ALIGN,VALIGN のサンプルです。
table_span.py
#! /usr/bin/python
#
# table_span.py
#
# Feb/13/2019
# ------------------------------------------------------------------
import sys
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.platypus import Table, TableStyle
from reportlab.platypus import Spacer
from reportlab.platypus import Paragraph
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
# ------------------------------------------------------------------
def table_first_proc(cc):
width, height = A4
data= [['こんにちは', 'Good\nMorning', 'CC', 'DD', '春'],
['おはよう\nございます。', '11', '12', '13', '夏'],
['今晩は', '21', '22', '23', '秋'],
['さようなら', '31', '32', '33', '冬']]
tt=Table(data,colWidths=(60*mm, 40*mm, 20*mm,20*mm,20*mm),rowHeights=16*mm)
tt.setStyle(TableStyle([('BACKGROUND',(2,1),(3,2),colors.cyan),
('TEXTCOLOR',(0,0),(1,-1),colors.red),
('FONT', (0, 0), (-1, -1), "TakaoMincho", 16),
('GRID', (0, 0), (4,3), 0.25, colors.black),
('ALIGN',(0,0),(1,3),'CENTER'),
('VALIGN',(0,0),(1,3),'MIDDLE'),
('SPAN', (0,0), (0,3)),
('SPAN', (1,0), (3,0)),
('SPAN', (4,0), (4,1)),
('SPAN', (1,1), (1,2)),
('SPAN', (2,1), (3,2)),
('SPAN', (4,2), (4,3)),
]))
tt.wrapOn(cc, width, height)
#
tt.drawOn(cc, 20*mm, 200*mm)
#
pp = Paragraph('Sample First',stylesheet['Title'])
pp.wrapOn(cc, 70*mm, 50*mm)
pp.drawOn(cc, 50*mm, 270*mm)
#
# ------------------------------------------------------------------
def table_second_proc(cc):
width, height = A4
data= [['テスト', 'Good\nAfternoon', 'CC', 'DD', '春'],
['おはよう\nございます。', '11', '12', '13', '夏'],
['今日は\n晴れています。', '21', '22', '23', '秋'],]
tt=Table(data,colWidths=(60*mm, 40*mm, 20*mm,30*mm,30*mm), rowHeights=20*mm)
tt.setStyle(TableStyle([('BACKGROUND',(1,1),(2,2),colors.magenta),
('TEXTCOLOR',(0,0),(1,-1),colors.blue),
('FONT', (0, 0), (-1, -1), "TakaoMincho", 20),
('GRID', (0, 0), (4, 4), 0.25, colors.black),
('SPAN', (0,0), (0,1)),
('SPAN', (2,0), (3,0)),
('SPAN', (1,1), (2,2)),
]))
style = ParagraphStyle(
name='Normal',
fontName='TakaoMincho',
fontSize=16,
)
tt.wrapOn(cc, width, height)
#
tt.drawOn(cc, 20*mm, 100*mm)
pp = Paragraph('サンプル その2',style)
pp.wrapOn(cc, 70*mm, 50*mm)
pp.drawOn(cc, 50*mm, 170*mm)
# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_pdf = sys.argv[1]
stylesheet = getSampleStyleSheet()
cc = canvas.Canvas(file_pdf, pagesize=A4)
path_font = '/usr/share/fonts/OTF/TakaoMincho.ttf'
pdfmetrics.registerFont(TTFont('TakaoMincho',path_font))
#
table_first_proc(cc)
#
table_second_proc(cc)
#
cc.showPage()
cc.save()
#
sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------