-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathD3将图片插入到excel中.py
49 lines (37 loc) · 1.25 KB
/
D3将图片插入到excel中.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
# @Time : 2020/1/19 10:17
# @Author : 结尾!!
# @FileName: 4将图片插入到excel中.py
# @Software: PyCharm
from PIL import Image
import os
import xlwings as xw
path='./alibaba_com.xlsx'
app = xw.App(visible=True, add_book=False)
wb = app.books.open(path)
sht = wb.sheets['alibaba_com_img']
img_list=sht.range("D2").expand('down').value
print(len(img_list))
def write_pic(cell,img_name):
path=f'./downloads_picture/{img_name}'
print(path)
fileName = os.path.join(os.getcwd(), path)
img = Image.open(path).convert("RGB")
print(img.size)
w, h = img.size
x_s = 70 # 设置宽 excel中,我设置了200x200的格式
y_s = h * x_s / w # 等比例设置高
sht.pictures.add(fileName, left=sht.range(cell).left, top=sht.range(cell).top, width=x_s, height=y_s)
if __name__ == '__main__':
for index,img in enumerate(img_list):
cell="B"+str(index+2)
if '@sc01' in img:
img_name = img[24:]
try:
write_pic(cell,img_name)
print(cell,img_name)
except:
print("没有找到这个img_name的图片",img_name)
wb.save()
wb.close()
app.quit()