Python读取excel中的图片完美解决方法

yipeiwu_com6年前Python基础

excel中有图片是很常见的,但是通过python读取excel中的图片没有很好的解决办法。

网上找了一种很聪明的方法,原理是这样的:

1、将待读取的excel文件后缀名改成zip,变成压缩文件。

2、再解压这个文件。

3、在解压后的文件夹中,就有excel中的图片。

4、这样读excel中的图片,就变成了读文件夹中的图片了,和普通文件一样,可以做各种处理。

解压后的压缩包如下:

python脚本如下:

 '''
 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')

总结

以上所述是小编给大家介绍的Python读取excel中的图片完美解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python3.5集合及其常见运算实例详解

本文实例讲述了Python3.5集合及其常见运算。分享给大家供大家参考,具体如下: 1、集合的定义:集合是一个无序的、无重复的数据的数据组合。 2、集合的特征: (1)去除重复元素:将一...

举例讲解Python中的迭代器、生成器与列表解析用法

迭代器:初探 上一章曾经提到过,其实for循环是可用于任何可迭代的对象上的。实际上,对Python中所有会从左至右扫描对象的迭代工具而言都是如此,这些迭代工具包括了for循环、列表解析、...

Python中Django框架下的staticfiles使用简介

django1.3新加入了一个静态资源管理的app,django.contrib.staticfiles。在以往的django版本中,静态资源的管理一向都是个问题。部分app发布的时候会...

实例讲解Python设计模式编程之工厂方法模式的使用

实例讲解Python设计模式编程之工厂方法模式的使用

工厂方法模式是简单工厂模式的进一步抽象和推广,它不仅保持了简单工厂模式能够向客户隐藏类的实例化过程这一优点,而且还通过多态性克服了工厂类过于复杂且不易于扩展的缺点。在工厂方法模式中,处于...

Python实现破解猜数游戏算法示例

本文实例讲述了Python实现破解猜数游戏算法。分享给大家供大家参考,具体如下: QQ群里的聊天机器人会发起猜数小游戏. 玩法如下: 1. 用户发 #猜数  &nbs...