2
6

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 3 years have passed since last update.

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

Last updated at Posted at 2019-02-16

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

sudo apt install python3-reportlab

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

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")
# ------------------------------------------------------------------
2
6
2

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
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?