python读取excel文件中嵌入式的图片
1、将待读取的excel文件后缀名改成zip,变成压缩文件。
2、再解压这个文件。
3、在解压后的文件夹中,就有excel中的图片。
4、这样读excel中的图片,就变成了读文件夹中的图片了,和普通文件一样,可以做各种处理。
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 | ''' File Name: readexcelimg Author: tim Date: 2018/7/26 19:52 Description: 读取excel中的图片,打印图片路径 先将excel转换成zip包,解压zip包,包下面有文件夹存放了图片,读取这个图片 ''' import os import zipfile # 判断是否是文件和判断文件是否存在 def isfile_exist(file_path): if not os.path.isfile(file_path): print ( "It's not a file or no such file exist ! %s" % file_path) return False else : return True # 修改指定目录下的文件类型名,将excel后缀名修改为.zip def change_file_name(file_path, new_type = '.zip' ): if not isfile_exist(file_path): return '' extend = os.path.splitext(file_path)[ 1 ] # 获取文件拓展名 if extend ! = '.xlsx' and extend ! = '.xls' : print ( "It's not a excel file! %s" % file_path) return False file_name = os.path.basename(file_path) # 获取文件名 new_name = str (file_name.split( '.' )[ 0 ]) + new_type # 新的文件名,命名为:xxx.zip dir_path = os.path.dirname(file_path) # 获取文件所在目录 new_path = os.path.join(dir_path, new_name) # 新的文件路径 if os.path.exists(new_path): os.remove(new_path) os.rename(file_path, new_path) # 保存新文件,旧文件会替换掉 return new_path # 返回新的文件路径,压缩包 # 解压文件 def unzip_file(zipfile_path): if not isfile_exist(zipfile_path): return False if os.path.splitext(zipfile_path)[ 1 ] ! = '.zip' : print ( "It's not a zip file! %s" % zipfile_path) return False file_zip = zipfile.ZipFile(zipfile_path, 'r' ) file_name = os.path.basename(zipfile_path) # 获取文件名 zipdir = os.path.join(os.path.dirname(zipfile_path), str (file_name.split( '.' )[ 0 ])) # 获取文件所在目录 for files in file_zip.namelist(): file_zip.extract(files, os.path.join(zipfile_path, zipdir)) # 解压到指定文件目录 file_zip.close() return True # 读取解压后的文件夹,打印图片路径 def read_img(zipfile_path): if not isfile_exist(zipfile_path): return False dir_path = os.path.dirname(zipfile_path) # 获取文件所在目录 file_name = os.path.basename(zipfile_path) # 获取文件名 pic_dir = 'xl' + os.sep + 'media' # excel变成压缩包后,再解压,图片在media目录 pic_path = os.path.join(dir_path, str (file_name.split( '.' )[ 0 ]), pic_dir) file_list = os.listdir(pic_path) for file in file_list: filepath = os.path.join(pic_path, file ) print (filepath) # 组合各个函数 def compenent(excel_file_path): zip_file_path = change_file_name(excel_file_path) if zip_file_path ! = '': if unzip_file(zip_file_path): read_img(zip_file_path) # main if __name__ = = '__main__' : compenent( '/Users/Desktop/test/people.xlsx' ) |
直接读取文件内容,不解压缩的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | with zipfile.ZipFile(filename, 'r' ) as zip_format_file: for image_name in zip_format_file.namelist(): # 将图片转换成Pillow中的图片对象 if (image_name[: 9 ] = = 'xl/media/' ) and ( len (image_name) > 9 ): #print(image_name) img_bytes = zip_format_file.read(image_name) #读取图片内容 md5 = hashlib.md5() md5.update(img_bytes) md5_value = md5.hexdigest() if md5_value not in pic_md5_list: pic_md5_list.append(md5_value) else : case_check.fail_reason_list( 'P0级测试结果的截图不允许重复,请替换重复的图片' ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2017-03-31 selenium 加载jquery
2017-03-31 获取html元素的XPath路径
2015-03-31 Test Double