解析表格是常用的技术。但是有些表各里面有图片怎么办?我想获得表格里面的图片,值得注意的是,图片没有位置信息,所以最好给图片进行编号,编号代表位置。

下面附上提取表格里面图片的代码。只要输出表格地址,和图片存放目录就行

import os,shutil
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,img_path):
if not isfile_exist(zipfile_path):
return False dir_path = os.path.dirname(zipfile_path) # 获取文件所在目录
file_name = os.path.basename(zipfile_path) # 获取文件名
unzip_dir = os.path.join(dir_path, str(file_name.split('.')[0]))
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,img_path)
shutil.move(filepath,img_path)
os.unlink(zipfile_path)
shutil.rmtree(unzip_dir) # 组合各个函数
def compenent(excel_file_path,img_path):
zip_file_path = change_file_name(excel_file_path)
if not os.path.exists(img_path):
os.mkdir(img_path)
if zip_file_path != '':
unzip_msg = unzip_file(zip_file_path)
if unzip_msg:
read_img(zip_file_path,img_path) # main
if __name__ == '__main__':
#excel地址
excel_path = 'C:\\Users\\SHEIN\\Desktop\\test\img.xlsx'
#图片目录
img_dir = 'C:\\Users\\SHEIN\\Desktop\\test\\imgage'
compenent(excel_path,img_dir)

python解析excel中图片+提取图片的更多相关文章

  1. Python读取excel中的图片

    作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...

  2. python 解析Excel

    python 解析Excel 公司背景:好吧LZ太懒了.略... 原由起因:公司老板发话要导出公司数据库中符合条件的数据,源数据有400万,符合条件的大概有70万左右吧. 最终目的:符合条件的数据并生 ...

  3. 用python在excel中读取与生成随机数写入excel中

    今天是我第一次发博客,就关于python在excel中的应用作为我的第一篇吧. 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再 ...

  4. Python解析excel文件并存入sqlite数据库

    最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...

  5. python解析excel

    import xlrd, base64excel_obj = xlrd.open_workbook(file_contents=base64.decodestring(filename)).#打开要解 ...

  6. 使用Python将Excel中的数据导入到MySQL

    使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...

  7. python读取excel中单元格的内容返回的5种类型

    (1) 读取单个sheetname的内容. 此部分转自:https://www.cnblogs.com/xxiong1031/p/7069006.html python读取excel中单元格的内容返回 ...

  8. 从web编辑器 UEditor 中单独提取图片上传,包含多图片单图片上传以及在线涂鸦功能

    UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码.(抄的...) UEditor是非常好用的富文 ...

  9. Python向excel中写入数据的方法 方法简单

    最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...

随机推荐

  1. Jmeter系列(3)- Jmeter安装目录介绍

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Jmeter安装目录说明 bin:包含 ...

  2. Java的自动装箱

    JDK5的新特性自动装箱:把基本类型转换为包装类类型自动拆箱:把包装类类型转换为基本类型 注意一个小问题: 在使用时,Integer x = null;代码就会出现NullPointerExcepti ...

  3. Mysql基础练习--实例

    修改字段名:alter table 表名 change 旧字段名 新字段名 新数据类型;--- 主键 ------------------------------------------------- ...

  4. MySQL之视图、触发器、函数、存储过程、索引

    1.视图 把某个查询语句(临时表)设置别名,日后方便使用,视图是虚拟的(不要在数据库里使用视图) #创建: create view v1(视图名称) as SQL #修改: alter view v1 ...

  5. wx.request出现400 bad request的问题

    wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content-type': 'a ...

  6. python学习06循环

    '''while''''''while 布尔表达式:冒号不能省略''''''1+2+3+...+10'''i=1sum1=0while i<=10: sum1+=i i+=1print(sum1 ...

  7. Js6利用class创建类

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Leo2DNT(雷傲论坛转DiscuzNT)1.0转换程序发布

    数据转换程序 雷傲论坛(Leobbs4.x) -> Discuz!NT V1.0    本转换程序基于Leobbs4.x设计     声明: 1.本程序只对数据作转换,不会对原来的雷傲论坛(数据 ...

  9. HDU 1874 畅通工程续 2008浙大研究生复试热身赛(2)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  10. The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 K题 center

    You are given a point set with nn points on the 2D-plane, your task is to find the smallest number o ...