1.根据ASIN将匹配图片,提前将A列的单元格设置为200*200;
最后插入的效果如下:
from PIL import Image
import os
import xlwings as xw
# app=xw.App(visible=True,add_book=False)
wb = xw.Book(r'F:\envs_pycharm\亚马逊下载图片\1-2020-下载图片-及变体\DetailPage\DetailPage\change_asin\吴银琳要求吊带包采集数据 sling bag.xlsx')
#居中 插入
import os
sht = wb.sheets['吴银琳要求吊带包采集数据 sling bag']
print(sht.range("B2").expand('down').value)
asin_list=sht.range("B2").expand('down').value
print(len(asin_list))
def write_pic(cell,asin):
path=f'./pic_0415/{asin}.jpg'
print(path)
fileName = os.path.join(os.getcwd(), path)
img = Image.open(path).convert("RGB")
print(img.size)
w, h = img.size
x_s = 70 # 设置宽 excel中,我设置了200x200的格式
y_s = h * x_s / w # 等比例设置高
sht.pictures.add(fileName, left=sht.range(cell).left, top=sht.range(cell).top, width=x_s, height=y_s)
if __name__ == '__main__':
for index,value in enumerate(asin_list):
cell="A"+str(index+2)
try:
write_pic(cell,value)
except:
print("没有找到这个ASIN的图片",value)
wb.save()