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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
from PIL import Image import openpyxl import openpyxl.styles from openpyxl.styles import PatternFill from openpyxl.utils import get_column_letter from progressbar import *
def RGB_to_Hex(rgb): """ RGB颜色转换成16进制颜色 :param rgb: :return: """ RGB = rgb.split(',') color = '' for i in RGB: num = int(i) color += str(hex(num))[-2:].replace('x', '0').upper() return color
def img2excel(img_path,excelout_path): """ 图片转换成excel :param img_path: 图片地址 :param excelout_path: excel保存地址 :return: """ img_src = Image.open(img_path) img_width = img_src.size[0] img_height=img_src.size[1] print("图片宽%s,高%s"%(img_width,img_height)) if img_src.mode != "RGB": img_src = img_src.convert('RGB')
str_strlist = img_src.load() wb=openpyxl.Workbook() wb.save(excelout_path) wb=openpyxl.load_workbook(excelout_path) sheet=wb["Sheet"] sheet.title="img2excel" cell_width = 1.0 cell_height = cell_width * (2.2862 / 0.3612) print("正在疯狂生成excel,请耐心等待...") widgets=['进度:',Percentage(),'',Bar('#'),'',Timer(),' ', ETA(), ' '] pb=ProgressBar(widgets=widgets) for w in pb(range(img_width)): for h in range(img_height): data = str_strlist[w,h] color=str(data).replace("(","").replace(")","") color=RGB_to_Hex(color) fille=PatternFill("solid",fgColor=color) sheet.cell(h+1,w+1).fill=fille print("生成完成,正在设置单元格格式...") for i in range(1, sheet.max_row+1): sheet.row_dimensions[i].height=cell_height for i in range(1, sheet.max_column+1): sheet.column_dimensions[get_column_letter(i)].width = cell_width print('格式设置完成,正在保存excel...') wb.save(excelout_path) img_src.close() print("保存excel成功!请打开[%s]查看"%excelout_path)
if __name__=='__main__': import sys,os if len(sys.argv)!=3: print("请输入图片地址和excel保存的地址\n" "例如命令行输入 python img2excel_user.py D:/result.png D:/outExcel.xlsx") sys.exit(0) else: img_virify=['.jpg','.png','.gif','.bmp','.jpeg','.jpe','.jfif'] excel_virify=['.xlsx','.xlsm','.xltx','.xltm']
img_path=sys.argv[1] excelout_path=sys.argv[2]
endName=os.path.splitext(img_path) if endName[1] not in img_virify: print("请选择支持的图片类型",img_virify) sys.exit(0)
endName_excel=os.path.splitext(excelout_path) if endName_excel[1] not in excel_virify: print("excel 格式不支持,请选择支持的格式",excel_virify) sys.exit(0) img2excel(r""+img_path+"",excelout_path)
|