├── Emily.jpg ├── Emily.xlsx ├── README.md ├── czj.jpg ├── czj.xlsx └── img2xls.py /Emily.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemodd/img2xls/f99f9ca7adea579e04a18ea631971e69a36e7594/Emily.jpg -------------------------------------------------------------------------------- /Emily.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemodd/img2xls/f99f9ca7adea579e04a18ea631971e69a36e7594/Emily.xlsx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # img2xls 2 | 3 | 将图片转换为Excel表格 4 | 5 | author:lemodd 6 | 7 | mail:lemodd@qq.com 8 | 9 | # 使用方法: 10 | 1.直接拖图片文件到img2xls.py上,或 11 | 12 | 2.命令行 13 | .\img2xls.py imgfile.jpg 14 | 15 | # 注意: 16 | 17 | 1.只支持jpg文件,尺寸最好不大于200*200 18 | 19 | 2.生成的文件为图片名称的.xlsx文件,用excel打开就看到了 20 | 21 | 3.使用之前需安装相关的模块,PIL,openpyxl,numpy 22 | 23 | -------------------------------------------------------------------------------- /czj.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemodd/img2xls/f99f9ca7adea579e04a18ea631971e69a36e7594/czj.jpg -------------------------------------------------------------------------------- /czj.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemodd/img2xls/f99f9ca7adea579e04a18ea631971e69a36e7594/czj.xlsx -------------------------------------------------------------------------------- /img2xls.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import numpy 3 | 4 | from openpyxl.styles import PatternFill,Border,Side 5 | from openpyxl import Workbook 6 | 7 | import sys 8 | 9 | if len(sys.argv)>1: 10 | img_file = sys.argv[1] 11 | else: 12 | img_file = 'czj.jpg' 13 | 14 | print(img_file) 15 | 16 | wb = Workbook() 17 | ws1 = wb.active 18 | ws1.title = 's1' 19 | 20 | border = Border(left=Side(style='thin',color='FF000000'), 21 | right=Side(style='thin',color='FF000000'), 22 | top=Side(style='thin',color='FF000000'), 23 | bottom=Side(style='thin',color='FF000000'), 24 | diagonal=Side(style='thin',color='FF000000'), 25 | diagonal_direction=0, 26 | outline=Side(style='thin',color='FF000000'), 27 | vertical=Side(style='thin',color='FF000000'), 28 | horizontal=Side(style='thin',color='FF000000')) 29 | 30 | image = Image.open(img_file) 31 | array = numpy.array(image) 32 | color_hex ='' 33 | row_count = len(array) 34 | col_count = len(array[0]) 35 | 36 | #此函数来自https://jalena.bcsytv.com/archives/2113 37 | def convent_column_to_char(column): 38 | 39 | if not isinstance(column, int): 40 | return column 41 | tStr = str() 42 | while column != 0: 43 | res = column % 26 44 | if res == 0: 45 | res = 26 46 | column -= 26 47 | tStr = chr(ord('A') + res - 1) + tStr 48 | column = column // 26 49 | return tStr 50 | 51 | 52 | print('正在转换,请稍候。。。') 53 | 54 | for i in range(row_count): 55 | for j in range(col_count): 56 | pixel = array[i][j] 57 | color_hex = '' 58 | for p in pixel: 59 | temp = hex(p)[2:].upper() 60 | if len(temp) == 1: 61 | temp = '0' + temp 62 | color_hex += temp 63 | 64 | fill = PatternFill(color_hex, color_hex, fill_type = 'solid') 65 | ws1.cell(i+1,j+1).fill = fill 66 | 67 | col_count = len(array[0]) 68 | 69 | for i in range(col_count): 70 | i += 1 71 | cname = convent_column_to_char(i) 72 | 73 | ws1.column_dimensions[cname].width = 2 74 | 75 | for j in range(row_count): 76 | j += 1 77 | cell_num = cname+str(j) 78 | ws1[cell_num].border=border 79 | ws1.row_dimensions[j].height = 12 80 | 81 | 82 | xls_file = img_file.split('.')[0]+'.xlsx' 83 | wb.save(xls_file) 84 | 85 | input('完成,回车继续.') 86 | --------------------------------------------------------------------------------