0
2

More than 1 year has passed since last update.

ReportLabのチートシート

Posted at

ReportLabのチートシートのチートシートです。

# ライブラリのインポート
from reportlab.pdfgen import canvas #PDFファイル作成
from reportlab.lib.pagesizes import A4, portrait #A4ポートレイト
from reportlab.pdfbase import pdfmetrics #メトリクス制御(フォントなど)
from reportlab.pdfbase.ttfonts import TTFont #フォント指定
import os

# ユーザのデスクトップのディレクトリを取得
file = "sample.pdf"
file_path = os.path.expanduser("~") + "/Desktop/" + file

# A4の新規PDFファイルを作成
page = canvas.Canvas(file_path, pagesize=portrait(A4))

# フォントの読み込み
pdfmetrics.registerFont(TTFont(Arial, "C:/Windows/Fonts/Arial.TTC"))

# フォントの設定(フォント、サイズ)
page.setFont("Arial", 20)

# 座標を指定して文字挿入(x, y, 文字)
page.drawString(200, 300, Hello World!”) #左端
page.drawCentredString(200, 200, Hello World!”) #中心
page.drawRightString(200, 100, Hello World!”) #右端

# PDFファイルを保存
page.save()

# 画像挿入
img_path = os.path.expanduser(../) + sample.ipg #ファイルパスを指定
page.drawImage(img_path, 200, 500) #画像挿入

# 線の描画
page.setStrokeColorRGB(1.0, 0.0, 1.0) #線の色を指定
page.setLineWidth(3) # 線の太さを指定
page.line(100, 100, 200, 200) # 線の描画

# 四角形の描画
page.rect(200, 200, 100, 50) # 四角形の描画

page.setFillColorRGB(1.0, 0.5, 0.3)
page.rect(300, 300, 100, 50, fill=True) # 塗りつぶし

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