python - 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']
但是如果我有数百张照片,这种方法会花费我很多时间。我怎样才能开发我的代码?我需要插入的所有照片都在同一个文件中。另外,我怎样才能调整我的照片大小?
解决方案
推荐阅读
- python - 如何使用 OpenCV + Python 检测 QR 码角标记(仅)?
- azure - MS Graph Daemon App - 获取承载令牌
- r - 在R中使用带有自定义函数的apply删除for循环
- django-rest-framework - 使用相同查找应用多个值时如何执行“或”?
- select - 过滤依赖于另一个值的值
- c# - 向 webservice 传递参数并接收值
- highcharts - 可以像这样设置 HIGHCHARTS 样式吗?
- go - 为什么比赛检测器在这里报告比赛条件?
- azure - 在 Azure 上发布特定的 appsettings.json 文件
- reactjs - React 似乎在修改变量名,然后抱怨它们不存在