import os #import xlsxwriter import cv2 import pandas as pd from openpyxl import * from openpyxl.drawing.image import Image import re #from PIL import Image
j=0 pic_file= r"D:\P&L Recovery business\新建文件夾\Pic"#圖片文件路徑 excel_file= r"D:\P&L Recovery business\臨時"#excel文件路徑 save_file= r"D:\P&L Recovery business\新建文件夾\Save"#文件保存路徑 newsize=(240,80) #圖片尺寸 for pic_name in os.listdir(pic_file): img = Image(pic_file+"/"+pic_name) img.width,img.height = newsize img_name = os.path.basename(pic_file+"/"+pic_name) print(img_name) for name in os.listdir(excel_file): for i in range (2): # i=0 excel_name = os.path.basename(excel_file+"/"+name) excel_name1 = excel_name.split('.')[0] # print(excel_name) global wb wb = load_workbook(excel_file+"/"+name) sheets = wb.sheetnames wt=wb[sheets[i]] print(wt) wt.add_image(img,'B1') i += 1 j += 1 wb.save(save_file+'/'+excel_name1+'.'+'xlsx') print("第%d個文件添加成功"%j)