1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

reportlab の Table のセルの結合をする

Posted at

次のようなPDFを作成するサンプルです。
pdf_feb1301.png

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")
# ------------------------------------------------------------------
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?