首页 技术 正文
技术 2022年11月18日
0 收藏 818 点赞 4,768 浏览 2424 个字

  作为Java程序员,Java自然是最主要的编程语言。但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现bug,需要重新修改打包。这就需要一门快速开发,方便运行调试的语言。python作为一门脚本语言,可以实现快速编写和快速调试等特性,很适合用于解决日常工作中小的工作任务。一般使用结构化的编程思路,按照流程一步一步的完成各个函数,就能快速的完成工作任务。

例如:

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

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

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

  2、再解压这个文件。

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

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

  解压后的压缩包中有xl/media目录,目录中有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开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289