Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Shopify API为新产品添加2张图片_Python_Shopify - Fatal编程技术网

Python Shopify API为新产品添加2张图片

Python Shopify API为新产品添加2张图片,python,shopify,Python,Shopify,我正在尝试使用api向我的新shopify商店添加2张图片。然而,我需要上传2张图片,而不仅仅是一张。这是我到目前为止,但它不工作,请告知 import shopify API_KEY = 'dsfsdsdsdsdsad' PASSWORD = 'sadsdasdasdas' shop_url = "https://%s:%s@teststore.myshopify.com/admin" % (API_KEY, PASSWORD) shopify.ShopifyResource.set

我正在尝试使用api向我的新shopify商店添加2张图片。然而,我需要上传2张图片,而不仅仅是一张。这是我到目前为止,但它不工作,请告知

import shopify    
API_KEY = 'dsfsdsdsdsdsad'
PASSWORD = 'sadsdasdasdas'

shop_url = "https://%s:%s@teststore.myshopify.com/admin" % (API_KEY, PASSWORD)
shopify.ShopifyResource.set_site(shop_url)



path = "audi.jpg"
path2 = "audi2.jpg"

new_product = shopify.Product()
new_product.title = "Audi pictures test "
new_product.body_html = "body of the page <br/><br/> test <br/> test"


variant = shopify.Variant({'price': 1.00, 'requires_shipping': False,'sku':'000007'})
new_product.variants = [variant]
image = shopify.Image()
image2 = shopify.Image()



with open(path, "rb") as f:
    filename = path.split("/")[-1:][0]
    filename2 = path2.split("/")[-1:][0]
    encoded = f.read()
    image.attach_image(encoded, filename=filename)
    image2.attach_image(encoded, filename=filename2)

new_product.images = [image,image2]
new_product.save()
导入shopify
API_键='DSAD'
密码='sadsdas'
shop_url=“https://%s:%s@teststore.myshopify.com/管理员“%(API_密钥,密码)
shopify.ShopifyResource.set\u站点(shop\u url)
path=“audi.jpg”
path2=“audi2.jpg”
新产品=shopify.product()
new_product.title=“奥迪图片测试”
new_product.body_html=“页面正文

test
variant=shopify.variant({'price':1.00,'requires_shipping':False,'sku':'000007')) 新产品变量=[变量] image=shopify.image() image2=shopify.Image() 打开(路径“rb”)作为f: filename=path.split(“/”[-1:[0] filename2=path2.split(“/”[-1:[0] encoded=f.read() 图像。附加图像(编码,文件名=文件名) 图像2.附加图像(编码,文件名=文件名2) new_product.images=[图像,图像2] 新产品。保存()
当您为两个图像传递相同的编码值时,此代码将仅向产品上载一个图像。我已经编辑了下面的代码,并分别为这两个图像创建了图像对象。这将把两张图片上传到Shopify

with open(path, "rb") as f:
    filename = path.split("/")[-1:][0]
    encoded = f.read()
    image.attach_image(encoded, filename=filename)

with open(path2, "rb") as f:
    filename2 = path2.split("/")[-1:][0]
    encoded = f.read()
    image2.attach_image(encoded, filename=filename2)