python解析excel中图片+提取图片
解析表格是常用的技术。但是有些表各里面有图片怎么办?我想获得表格里面的图片,值得注意的是,图片没有位置信息,所以最好给图片进行编号,编号代表位置。
下面附上提取表格里面图片的代码。只要输出表格地址,和图片存放目录就行
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中图片+提取图片的更多相关文章
- Python读取excel中的图片
作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...
- python 解析Excel
python 解析Excel 公司背景:好吧LZ太懒了.略... 原由起因:公司老板发话要导出公司数据库中符合条件的数据,源数据有400万,符合条件的大概有70万左右吧. 最终目的:符合条件的数据并生 ...
- 用python在excel中读取与生成随机数写入excel中
今天是我第一次发博客,就关于python在excel中的应用作为我的第一篇吧. 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再 ...
- Python解析excel文件并存入sqlite数据库
最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...
- python解析excel
import xlrd, base64excel_obj = xlrd.open_workbook(file_contents=base64.decodestring(filename)).#打开要解 ...
- 使用Python将Excel中的数据导入到MySQL
使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...
- python读取excel中单元格的内容返回的5种类型
(1) 读取单个sheetname的内容. 此部分转自:https://www.cnblogs.com/xxiong1031/p/7069006.html python读取excel中单元格的内容返回 ...
- 从web编辑器 UEditor 中单独提取图片上传,包含多图片单图片上传以及在线涂鸦功能
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码.(抄的...) UEditor是非常好用的富文 ...
- Python向excel中写入数据的方法 方法简单
最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...
随机推荐
- Go gRPC进阶-proto数据验证(九)
前言 上篇介绍了go-grpc-middleware的grpc_zap.grpc_auth和grpc_recovery使用,本篇将介绍grpc_validator,它可以对gRPC数据的输入和输出进行 ...
- Maven+JSP+SSM+Mysql+C3P0实现的学生管理系统
项目简介 项目来源于:https://gitee.com/wu_yun_long/student_management_system 本系统是基于Maven+JSP+SSM+Mysql+C3P0实现的 ...
- 文本序列化【通用】word2sequence,文本序列字典保存
''' 文本序列化 ''' class WordSequence(): UNK_TAG = "<UNK>" PAD_TAG = "<PAD>&qu ...
- 用Python的Plotly画出炫酷的数据可视化(含各类图介绍,附代码)
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 我被狗咬了 在谈及数据可视化的时候,我们通常都会使用到matplo ...
- What does __GNUC__ mean?
It indicates that I'm a GNU compiler and you can use GNU extensions. https://stackoverflow.com/quest ...
- mac OS 配置Apache服务器
Mac自带了Apache环境 查看Apache版本 sudo apachectl -v 在终端输入:sudo apachectl start 在浏览器输入"http://localhost& ...
- VR全景视图 Google VrPanoramaView
2019独角兽企业重金招聘Python工程师标准>>> 一.背景简介 Welcome to VR at Google 进入Google VR主页,发现官方给我们提供了两套解决观看VR ...
- Android多线程下载远程图片
修改后的代码 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 ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- 数学--数论--Find Integer(勾股数定理)
Problem Description people in USSS love math very much, and there is a famous math problem give you ...