如何更改图像';s定位:“;使用单元格移动和调整大小”;在Excel中使用openpyxl
如何使用Openpyxl在Python中选择此图像属性?我需要这个,所以如果我过滤Excel文档,图像也会被过滤 我调用了一个函数:如何更改图像';s定位:“;使用单元格移动和调整大小”;在Excel中使用openpyxl,excel,image,openpyxl,Excel,Image,Openpyxl,如何使用Openpyxl在Python中选择此图像属性?我需要这个,所以如果我过滤Excel文档,图像也会被过滤 我调用了一个函数:insert\u image(ws,url,cell\u picture,64,66) ws是我正在添加的工作表 url就是图像 细胞\描绘细胞 64和66表示宽度和高度 我的职能是: def insert_image(ws, image_url, cell, width = 101, height= 129): # load image image_
insert\u image(ws,url,cell\u picture,64,66)
ws是我正在添加的工作表
url就是图像
细胞\描绘细胞
64和66表示宽度和高度
我的职能是:
def insert_image(ws, image_url, cell, width = 101, height= 129):
# load image
image_path = "image.jpg"
try:
urllib.request.urlretrieve(image_url, image_path)
img = Image.open(image_path)
except:
print ('Picture ' + image_url + ' not found')
return
max_width, max_height = width , height
cell_ratio = float(max_height) / max_width
img_ratio = float(img.size[1])/img.size[0]
if (cell_ratio<img_ratio):
hpercent = (max_height/float(img.size[1]))
wsize = int((float(img.size[0])*float(hpercent)))
img = img.resize((wsize,max_height-1), PIL.Image.ANTIALIAS)
else:
wpercent = (max_width/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((max_width,hsize), PIL.Image.ANTIALIAS)
# remove image
if os.path.isfile(image_path): os.remove(image_path)
# Create openpyxl img instace
op_img = imge(img)
# Create image anchor
op_img.anchor(cell, anchortype='oneCell')
op_img.drawing.top = 1
#op_img.drawing.left = 1
# Add image to cell
ws.add_image(op_img)
#ws.insert_image(cell,op_img,{'positioning':1})
# print ('Image inserted')
return
def insert_image(ws,image_url,单元格,宽度=101,高度=129):
#加载图像
image\u path=“image.jpg”
尝试:
urllib.request.urlretrieve(图像\ url,图像\路径)
img=Image.open(图像路径)
除:
打印('图片'+图像\u url+'未找到')
返回
最大宽度,最大高度=宽度,高度
单元格比率=浮动(最大高度)/最大宽度
img_比率=浮动(img.size[1])/img.size[0]
if(单元比)