图像未显示在excell单元中(python和openpyxl)

时间:2018-08-01 06:40:35

标签: python image resize openpyxl

我正在尝试使用python和openpyxl的名为add_image的函数创建脚本,该函数会将调整大小后的照片添加到单元格中,为此,我正在使用 以下代码:

from PIL import Image
from openpyxl import load_workbook
from openpyxl import Workbook

wb = load_workbook('file.xlsx')
ws = wb.active

max_row = ws.max_row

########## SET DIMENSIONS FOR 'F' cells#######
ws.column_dimensions['F'].width=200 
for x in range (2,max_row+1):
    ws.row_dimensions[x].height=200
##############################################
img = Image.open(file)
bound_width_height = (195, 150)
img.thumbnail(bound_width_height, Image.ANTIALIAS)
nimg = openpyxl.drawing.image.Image(img)
ws.add_image(nimg,'F3')

wb.save('test.xlsx')
print('done!')

但是当我观看test.xlsx文件时,我看到了This image cannot curently be displayed.

结果: https://imgur.com/a/mjgL4hn

任何想法为何?

0 个答案:

没有答案