Python
Python で画像と PDF を作成する
Python SDK で、テンプレートから画像、PDF、ホスト済みの画像 URL を生成します。
各メソッドは、テンプレート ID とキーワード引数を受け取ります。テンプレート ID 以外の引数は、すべて省略できます。
画像を作成する
create_image はテンプレートをレンダリングし、画像のバイト列を返します。
テンプレート ID。たとえば tpl_xxx です。
テンプレートのレイヤーに設定する値。各項目は name でレイヤーを指定します。指定しなかったレイヤーは、テンプレートの値のままです。各レイヤーが受け付けるフィールドは、Modifications にあります。
画像の形式。png、jpeg、webp のいずれかです。
result = client.create_image(
"tpl_xxx",
modifications=[
{"name": "headline", "text": "Launch promo"},
{"name": "price", "text": "$29"},
],
format="webp",
)
if "error" in result:
raise RuntimeError(result["error"]["message"])
with open("launch-promo.webp", "wb") as file:
file.write(result["result"])
PDF を作成する
create_pdf は、同じ template_id と modifications を受け取ります。format 引数はありません。
result = client.create_pdf(
"tpl_invoice",
modifications=[
{"name": "customer", "text": "Acme Studio"},
{
"name": "line-items",
"columns": ["Item", "Qty", "Total"],
"rows": [{"Item": "Automate plan", "Qty": 1, "Total": "$29.00"}],
},
],
)
if "error" in result:
raise RuntimeError(result["error"]["message"])
with open("invoice.pdf", "wb") as file:
file.write(result["result"])
保存済み画像を作成する
create_stored_image は、画像をレンダリングして Bannerify の CDN に保存し、バイト列の代わりに URL を返します。URL は https://images.bannerify.co/files/ で始まります。リンクをメール、データベースの行、ノーコードツールに入れたいときに使います。
create_image と同じ引数を受け取ります。
result = client.create_stored_image(
"tpl_xxx",
modifications=[{"name": "headline", "text": "Welcome back"}],
)
if "error" in result:
raise RuntimeError(result["error"]["message"])
print(result["result"])
Python SDK は s3Config を送れません。ファイルを自分のバケットに保存するには、保存済み画像を作成するを、REST で呼び出してください。
結果を処理する
3 つのメソッドは、いずれも {"result": ...} または {"error": ...} を返します。エラーを処理するを参照してください。