You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Figma API上传本地图片至文件却无法显示的问题求助

Python调用Figma API上传本地图片至文件却无法显示的问题求助

我现在遇到个棘手的问题:想用Python脚本把本地的几张图片上传到Figma文件里,脚本能正常识别到目标文件、节点以及对应的ID,但图片就是没法成功显示在Figma文件里,而且全程没有报错提示。我已经用python -m http.server 8000启动了本地服务器,图片都是通过这个本地服务的URL来上传的。

如果有人知道怎么解决这个问题,麻烦给点思路,或者需要更多细节的话也可以问我!

以下是我用的代码:

import requests
import os
import traceback

# Personal Figma token
TOKEN = "token"
FILE_KEY = "file key"

# Auth
HEADERS = {
    "X-Figma-Token": TOKEN
}

# Uploading to Figma
def upload_image_to_node(node_id, image_url):
    url = f"https://api.figma.com/v1/images/{FILE_KEY}"
    json_data = {
        "requests": [
            {
                "node_id": node_id,
                "fill": {
                    "type": "IMAGE",
                    "image_url": image_url,
                    "scale_mod": "FILL"
                }
            }
        ]
    }
    response = requests.post(url, headers=HEADERS, json=json_data)
    if response.status_code == 200:
        print(f"Image {image_url} successfully uploaded to node {node_id}.")
    else:
        print(f"Error: {response.status_code}, {response.text}")

# Main
def process_images(input_folder, node_id):
    base_url = "http://localhost:8000"
    for filename in os.listdir(input_folder):
        if filename.endswith((".png", ".jpg")):
            image_url = f"{base_url}/{filename}"
            upload_image_to_node(node_id, image_url)

try:
    node_id = "0:1" # Main node in Figma
    process_images("out/", node_id) # out/ is where images stored
except Exception as e:
    print("Error occured:")
    traceback.print_exc()
    input("Press Enter to quit..")

备注:内容来源于stack exchange,提问作者Erick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 18:03:01