目录
通过excel表单给图片文件重命名
需求
excel表单如下,其中ksh表示考生号,sfzh表示身份证号
图片文件格式如下。图片文件是身份证号+“jpg”格式命名的。
我们的任务是通过图片名的身份证号,找到对应的考生号,然后给图片重命名为:考生号+“JPG”。因为我们的图片文件是要多余实际被录取的考生的,所以图片文件中有一些文件是在excel表单中找不到的,因此我们要将可以在excel表单找到信息的考生照片重命名后移动到新的文件夹。
代码
import os
import shutil
import pandas as pd
def mycopyfile(srcfile, dstpath): # 复制函数
if not os.path.isfile(srcfile):
print("%s not exist!" % (srcfile))
else:
fpath, fname = os.path.split(srcfile) # 分离文件名和路径
if not os.path.exists(dstpath):
os.makedirs(dstpath) # 创建路径
shutil.copy(srcfile, dstpath + fname) # 复制文件
print("copy %s -> %s" % (srcfile, dstpath + fname))
#得到文件夹下所有文件的名字
filePath = '.\\图片'
document = os.listdir(filePath)
path = "E:\\桌面\\python_wolk\\公司的需求\\图片"
path1 = "E:\\桌面\\python_wolk\\公司的需求\\temp"
# os.rename(path +"\\"+document1[0], path +"\\"+"1.JPG")
df = pd.read_excel(".\\student_info.xls")
for s in document:
print(s)
s1 = s.split(".")[0]
for i in range(len(df.values)):
if df.values[i][1] == s1:
os.rename(path + "\\" + s, path + "\\" + str(df.values[i][0])+".JPG")
mycopyfile(path + "\\" + str(df.values[i][0])+".JPG",path1+"\\")
break
将不同学校的学生转移到不同的文件夹下
需求
excel表格和图片文件如下
如图所示,图片文件是按照考生号+“JPG”格式命名的,我们的需求是依据图片名字,找到这个学生对应的学院,将这个学院的所有图片文件保存到一个文件夹中。
代码
import os
import shutil
import pandas as pd
def mycopyfile(srcfile, dstpath): # 复制函数
if not os.path.isfile(srcfile):
print("%s not exist!" % (srcfile))
else:
fpath, fname = os.path.split(srcfile) # 分离文件名和路径
if not os.path.exists(dstpath):
os.makedirs(dstpath) # 创建路径
shutil.copy(srcfile, dstpath + fname) # 复制文件
print("copy %s -> %s" % (srcfile, dstpath + fname))
#得到文件夹下所有文件的名字
filePath = '.\\imgs'
document = os.listdir(filePath)
# document1 = os.listdir('.\\test')
path = "E:\\桌面\\python_wolk\\公司的需求\\imgs"
path1 = "E:\\桌面\\python_wolk\\公司的需求\\temp"
# os.rename(path +"\\"+document1[0], path +"\\"+"1.JPG")
df = pd.read_excel(".\\专升本学生.xls")
for i in range(len(df.values)):
if df.values[i][2] == '阜阳幼儿师范高等专科学校':
for s in document:
s1 = s.split(".")[0]
if str(df.values[i][1]) == s1:
mycopyfile(path + "\\" + str(df.values[i][1]) + ".JPG", path1 + "\\")
#print(str(df.values[i][1])+" "+s1+" "+str(df.values[i][0]))
break
处理二级文件下的excel表格
需求
这两天接了公司的一个需求,要读取二级文件目录下的xlsx表格并将需要的信息存储在一张表格里。然后导入到数据库中。
第一级目录如下图所示:
二级目录如下,每一个小的文件夹有用户的表格
每个表格里有每个人的相关信息
设第一级目录中文件的个数为m,第二级目录下excel文件的个数为n,我们的任务就是就提取这m*n个excel文件中的数据。
思路:
我们先遍历得到所有二级文件名,存在一个数组里,再获取每个二级文件中的excel名字,拼接一下字符串,得到正确的excel路径,再读入到df二维数组中。
观察excel表格结构,将我们的数据取出并保存
代码:
import os
import pandas as pd
import datetime
import numpy as np
import math
import pandas as pd
import numpy as np
import xlwt
import matplotlib.pyplot as plt
filePath = 'E:\\桌面\\信息采集数据'
document=os.listdir(filePath)
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet('基本信息',cell_overwrite_ok=False)
cnt=1
for name1 in document:
document_path="E:/桌面/信息采集数据/"+name1
print(document_path)
# 遍历某目录下的Excel文件名字,加入列表
list1 = []
for file in os.listdir(document_path):
if file.endswith("xlsx") or file.endswith("xls"):
list1.append(file)
for name in list1:
print(document_path+"/"+name)
if name[0:2]=="~$":
name=name[2:]
df=pd.read_excel(document_path+"/"+name)
s=[]
for i in range(16):
for j in range(1,9,2):
s.append(df.values[i][j])
for i in range(len(s)):
sheet.write(cnt,i,s[i])
cnt+=1
savepath = 'E:/桌面/学习经历.xlsx'
book.save(savepath)
结果:
完整的代码我上传到了这里
(2条消息) 数据处理excel脚本.zip-统计分析文档类资源-CSDN文库
本文含有隐藏内容,请 开通VIP 后查看