0
5

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 を作成するサンプルです。
table_feb11.png

table_position.py
#! /usr/bin/python
# ------------------------------------------------------------------
#	table_position.py
#
#						Feb/11/2018
# ------------------------------------------------------------------
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))

cc = canvas.Canvas('example.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()
# ------------------------------------------------------------------
0
5
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
0
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?