5
8

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 で日本語を使う

Last updated at Posted at 2019-02-02

reportlab の Table で日本語を使う方法です。
次のような PDF を作成します。
reportlab_feb0201.png

simple_table.py
#! /usr/bin/python
#
#	simple_table.py
#
#						Feb/02/2019
# ------------------------------------------------------------------
import sys
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_pdf = sys.argv[1]
 
doc = SimpleDocTemplate(file_pdf, pagesize=letter)

pdfmetrics.registerFont(TTFont('TakaoMincho','/usr/share/fonts/OTF/TakaoMincho.ttf'))

elements = []
 
data= [['こんにちは', 'BB', 'CC', 'DD', ''],
       ['おはよう', '11', '12', '13', ''],
       ['今晩は', '21', '22', '23', ''],
       ['さようなら', '31', '32', '33', '']]
tt=Table(data)
tt.setStyle(TableStyle([('BACKGROUND',(1,1),(-2,-2),colors.cyan),
                       ('TEXTCOLOR',(0,0),(1,-1),colors.red),
			('FONT', (0, 0), (-1, -1), "TakaoMincho", 20),]))
elements.append(tt)
#
doc.build(elements)

sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------

実行方法

./simple_table.py out01.pdf

もう少し複雑な次のような PDF を作成するサンプルです。
table_feb0202.png

table_two.py
#! /usr/bin/python
#
#	table_two.py
#
#						Feb/02/2019
# ------------------------------------------------------------------
import sys
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, 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
# ------------------------------------------------------------------
def table_first(elements):
	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',(1,1),(-2,-2),colors.cyan),
		('TEXTCOLOR',(0,0),(1,-1),colors.red),
		('FONT', (0, 0), (-1, -1), "TakaoMincho", 16),
		('GRID', (0, 0), (4, 4), 0.25, colors.black),]))

	elements.append(Paragraph('Sample First',stylesheet['Title']))
	elements.append(tt)
	elements.append(Spacer(1,10*mm))
#
# ------------------------------------------------------------------
def table_second(elements):
	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),]))

	style = ParagraphStyle(
       		name='Normal',
	        fontName='TakaoMincho',
       		fontSize=16,
	)

	elements.append(Paragraph('サンプル その2',style))
	elements.append(Spacer(1,10*mm))
	elements.append(tt)
# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_pdf = sys.argv[1]

stylesheet = getSampleStyleSheet()
 
doc = SimpleDocTemplate(file_pdf, pagesize=letter)

path_font = '/usr/share/fonts/OTF/TakaoMincho.ttf'
pdfmetrics.registerFont(TTFont('TakaoMincho',path_font))
#

elements = []
 
table_first(elements)

table_second(elements)
#
doc.build(elements)

sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------

参考ページ
reportlab の Table の表示位置をコントロールする
reportlab の Table のセルの結合をする

5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?