gpt4 book ai didi

excel - 如何更改图像的定位 : "Move and size with cells" in Excel with openpyxl

转载 作者:行者123 更新时间:2023-12-04 19:45:22 26 4
gpt4 key购买 nike

enter image description here

如何使用 Openpyxl 使用 Python 选择此图像属性?我需要这个,所以如果我过滤 Excel 文档,图像也会被过滤。

我调用了一个函数:insert_image(ws, url, cell_picture, 64, 66)ws 是我要添加的工作表
网址是图片
cell_picture 细胞
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

最佳答案

TwoCellAnchor(editAs="twoCell", ...)示例代码:

# *- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl.drawing.image import Image
from openpyxl.drawing.spreadsheet_drawing import AnchorMarker, TwoCellAnchor


wb = Workbook()
ws = wb.active

ws.column_dimensions["A"].width = 18
ws.row_dimensions[1].height = 140
col, row = 0, 0
offset = 30000
img = Image("test.jpg")
_from = AnchorMarker(
col=col,
row=row,
colOff=offset,
rowOff=offset,
)
to = AnchorMarker(
col=col + 1,
row=row + 1,
colOff=-offset,
rowOff=-offset,
)
img.anchor = TwoCellAnchor(editAs="twoCell", _from=_from, to=to)
ws.add_image(img)
wb.save("test.xlsx")

关于excel - 如何更改图像的定位 : "Move and size with cells" in Excel with openpyxl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com