from openpyxl import load_workbook
from PIL import Image
from openpyxl import load_workbook
wb = load_workbook( r'C:\Users\Administrator\Downloads\output1111.xlsx' )
ws = wb[ wb. sheetnames[ 0 ] ]
for image in ws. _images:
data= image. anchor. _from
if image. format == 'gif' :
with open ( f'C:\\Users\\Administrator\\Desktop\\hello\\row_ { data. row} _col_ { data. col} .gif' , 'wb' ) as output_gif:
output_gif. write( image. _data( ) )
if image. format == 'png' :
with open ( f'C:\\Users\\Administrator\\Desktop\\hello\\row_ { data. row} _col_ { data. col} .png' , 'wb' ) as output_gif:
output_gif. write( image. _data( ) )
print ( image. anchor. _from)
if image. format == 'jpg' :
with open ( f'C:\\Users\\Administrator\\Desktop\\hello\\row_ { data. row} _col_ { data. col} .jpg' , 'wb' ) as output_gif:
output_gif. write( image. _data( ) )
print ( image. anchor. _from)
优化更新后有多张图片
from openpyxl import load_workbook
wb = load_workbook( r'E:\odoo\odoo16\output1111.xlsx' )
ws = wb[ wb. sheetnames[ 0 ] ]
i = 1
def find_fially ( picture_name, row_list) :
seq = int ( picture_name. split( '_' ) [ 1 ] )
picture_name = f' { name} _ { seq + 1 } '
if picture_name in row_list:
return find_fially( picture_name, row_list)
return picture_name
row_list = [ ]
for image in ws. _images:
data = image. anchor. _from
name = ws. cell( row= data. row + 1 , column= 1 ) . _value
picture_name = f' { name} _ { i} '
if picture_name not in row_list:
i = 1
else :
picture_name = find_fially( picture_name, row_list)
row_list. append( picture_name)
with open ( f'E:\\ { picture_name} . { image. format } ' , 'wb' ) as output_gif:
output_gif. write( image. _data( ) )