首页 > 解决方案 > python在Excel中插入多张图片

问题描述

我有数百张想插入 Excel 的照片,我有这样的代码:

import xlsxwriter
#creat an new excel file and add a worksheet
workbook = xlsxwriter.Workbook("/Users/sherry/Desktop/HR/rending/new/total/All/insert_images.xlsx")
worksheet = workbook.add_worksheet()
#resize cells
worksheet.set_column('B1:B4', 7)
worksheet.set_default_row(45)

#image list
images = ['/Users/sherry/Desktop/HR/rending/new/total/All/5138221.jpg',
         '/Users/sherry/Desktop/HR/rending/new/total/All/5101221.jpg',
'/Users/sherry/Desktop/HR/rending/new/total/All/2311021.jpg',
'/Users/sherry/Desktop/HR/rending/new/total/All/5101221.jpg']

#insert images 
image_row = 0
image_col = 1
for image in images:
    worksheet.insert_image(image_row, image_col,
                          image,
                          {'x_scale': 0.5, 'y_scale':0.5,
                          'x_offset':5, 'y_offset':5,
                          'positioning': 1})
    #positioning =1 allows move and size with cells (may not always perform as expected)
    image_row +=1
titles = ['5138221.jpg','5101221.jpg','2311021.jpg','51012219.jpg']

#insert titles
title_row = 0
title_col = 0
for title in titles:
    worksheet.write(title_row, title_col, title)
    title_row += 1
workbook.close()

如果我的照片很少,我可以这样做:

#image list
images = ['/Users/sherry/Desktop/HR/rending/new/total/All/5138221.jpg',
         '/Users/sherry/Desktop/HR/rending/new/total/All/5101221.jpg',
'/Users/sherry/Desktop/HR/rending/new/total/All/2311021.jpg',
'/Users/sherry/Desktop/HR/rending/new/total/All/5101221.jpg']

#insert images 
image_row = 0
image_col = 1
for image in images:
    worksheet.insert_image(image_row, image_col,
                          image,
                          {'x_scale': 0.5, 'y_scale':0.5,
                          'x_offset':5, 'y_offset':5,
                          'positioning': 1})
    #positioning =1 allows move and size with cells (may not always perform as expected)
    image_row +=1
titles = ['5138221.jpg','5101221.jpg','2311021.jpg','51012219.jpg']

但是如果我有数百张照片,这种方法会花费我很多时间。我怎样才能开发我的代码?我需要插入的所有照片都在同一个文件中。另外,我怎样才能调整我的照片大小?

标签: pythonexcel

解决方案


推荐阅读