次のような PDF を作成するサンプルです。
プログラム
table_position.py
# ! /usr/bin/python
# ------------------------------------------------------------------
# table_position.py
#
# Nov/25/2025
# ------------------------------------------------------------------
import sys
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfgen import canvas
from reportlab.platypus import Image, Paragraph, Table
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
# ------------------------------------------------------------------
fontname_g = "HeiseiKakuGo-W5"
pdfmetrics.registerFont(UnicodeCIDFont(fontname_g))
file_pdf = sys.argv[1]
cc = canvas.Canvas(file_pdf, pagesize=A4)
width, height = A4
cc.setFont("HeiseiKakuGo-W5", 16)
str_out = "こんにちは"
cc.drawString(100, 730, str_out)
data = [["テスト", 2, 3], ["日本語", 1, 3], [3, 2, 10]]
table = Table(data, colWidths=20*mm)
table.setStyle([("VALIGN", (0,0), (-1,-1), "MIDDLE"),
("ALIGN", (0,0), (-1,-1), "CENTER"),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
('FONT', (0, 0), (-1, -1), "HeiseiKakuGo-W5", 16),
])
#
table.wrapOn(cc, width, height)
#
table.drawOn(cc, 140*mm, 250*mm)
table.drawOn(cc, 75*mm, 225*mm)
table.drawOn(cc, 10*mm, 200*mm)
#
styles = getSampleStyleSheet()
my_style = styles["Normal"]
my_style.name = "bonlife"
my_style.fontName = "HeiseiKakuGo-W5"
my_style.fontSize=16
ptext = "これはサンプルです。"
pp = Paragraph(ptext, style=my_style)
pp.wrapOn(cc, 70*mm, 50*mm) # size of 'textbox' for linebreaks etc.
pp.drawOn(cc, 50*mm, 190*mm) # position of text / where to draw
cc.showPage()
cc.save()
# ------------------------------------------------------------------
実行方法
./table_position.py out01.pdf
確認したバージョン
$ python --version
Python 3.13.7
