网上阅卷切割的图片是以考号命名,然后是每个考生的切割图片
如图:
该文件实现功能是将图片重新分类命名组织如下:
输入:1、切割图片的源目录:001文件夹路径
:2、考生考号exl表要求格式如下
关键是第一列,第二列,第五列必须为学号,姓名,班级,且第一行为标题行
输出:1、以班级-图片块-姓名分类组织的文件夹
所用模块:操作excel-win32com.client
: 操作文件与目录-os、shutil
: 窗体-wx
运行效果:
# -*- coding: cp936 -*-
#----------------------------
# 文件名称: EnglishMin.py
# 作 者: ghf
# 日 期: 2014-3-24
# 功 能:1、将网上阅卷切割的英语二卷,作文题目
# 按照班级分开,并且以学生姓名命名
# 版 本:v0.1
#
# 日 期:2014-3-28
# 版 本:v0.2
# 功 能:1、将网上阅卷切割的试卷,按照班级命名
# -->切割块命名的文件夹-->姓名命名图片
#----------------------------
import wx
import os
import sys
import shutil#删除非空目录
import win32com.client
#修改系统默认字体
reload(sys)
sys.setdefaultencoding( 'cp936' )
class MainFrame(wx.Frame):
#初始构造函数
def __init__(self, parent, id=-1, title='EnglishM',
position=wx.DefaultPosition, size=(500, 400)):
self.frame = wx.Frame.__init__(self, parent, id, title, position, size)
self.InitUI()
self.Show()
pass
def InitUI(self):
#容器布局用
self.panel = wx.Panel(self, -1)
vBox = wx.BoxSizer(wx.VERTICAL)
hSrcPicPathBox = wx.BoxSizer(wx.HORIZONTAL)
hStuPathBox = wx.BoxSizer(wx.HORIZONTAL)
hDesPicPathBox = wx.BoxSizer(wx.HORIZONTAL)
#---创建控件
#学生切割图片保存目录
self.labSrcPicPath = wx.StaticText(self.panel, -1, '请选择图片源目录:')
self.txtSrcPicPath = wx.TextCtrl(self.panel, -1, value='')
self.btnSrcPicPath = wx.Button(self.panel, -1, '浏览')
#学生源文件目录
self.labStuPath = wx.StaticText(self.panel, -1, '请选择学生考号文件:')
self.txtStuPath = wx.TextCtrl(self.panel, -1, value='')
self.btnStuPath = wx.Button(self.panel, -1, '浏览')
#学生目的图片保存目录
self.labDesPicPath = wx.StaticText(self.panel, -1, '请选择保存图片目录:')
self.txtDesPicPath = wx.TextCtrl(self.panel, -1, value='')
self.btnDesPicPath = wx.Button(self.panel, -1, '浏览')
#生成结果显示
self.txtResult = wx.TextCtrl(self.panel, -1, value='', style=wx.TE_MULTILINE)
#开始生成
self.btnBeginCreate = wx.Button(self.panel, -1, '开始生成')
#---绑定事件
self.Bind(wx.EVT_BUTTON, self.OnBtnSrcPic, self.btnSrcPicPath)
self.Bind(wx.EVT_BUTTON, self.OnBtnStu, self.btnStuPath)
self.Bind(wx.EVT_BUTTON, self.OnBtnDesPic, self.btnDesPicPath)
self.Bind(wx.EVT_BUTTON, self.OnBtnBeginCreate, self.btnBeginCreate)
#添加布局管理器
hSrcPicPathBox.Add(self.labSrcPicPath, 0, wx.EXPAND, 2)
hSrcPicPathBox.Add(self.txtSrcPicPath, 0, wx.EXPAND, 2)
hSrcPicPathBox.Add(self.btnSrcPicPath, 0, wx.EXPAND, 2)
hStuPathBox.Add(self.labStuPath, 0, wx.EXPAND, 2)
hStuPathBox.Add(self.txtStuPath, 0, wx.EXPAND, 2)
hStuPathBox.Add(self.btnStuPath, 0, wx.EXPAND, 2)
hDesPicPathBox.Add(self.labDesPicPath, 0, wx.EXPAND, 2)
hDesPicPathBox.Add(self.txtDesPicPath, 0, wx.EXPAND, 2)
hDesPicPathBox.Add(self.btnDesPicPath, 0, wx.EXPAND, 2)
vBox.Add(hSrcPicPathBox, 0, wx.EXPAND|wx.ALL, 10)
vBox.Add(hStuPathBox, 0, wx.EXPAND|wx.ALL, 10)
vBox.Add(hDesPicPathBox, 0, wx.EXPAND|wx.ALL, 10)
vBox.Add(self.btnBeginCreate, 0)
vBox.Add(self.txtResult, 2, wx.EXPAND|wx.ALL, 10)
self.panel.SetSizer(vBox)
pass
#--事件处理函数
#单击浏览源图片
def OnBtnSrcPic(self, event):
dirSrcPicPath = wx.DirDialog(self.panel, "选择源图片目录")
if dirSrcPicPath.ShowModal() == wx.ID_OK:
#print dirSrcPicPath.GetPath()
self.txtSrcPicPath.SetValue(dirSrcPicPath.GetPath())
dirSrcPicPath.Destroy()
pass
#单击浏览学生文件
def OnBtnStu(self, event):
filesFilter = "Excel Files (*.xls)|*.xls"
fileDialog = wx.FileDialog(self, message ="选择学生名单exl文件",
wildcard = filesFilter, style = wx.FD_OPEN)
dialogResult = fileDialog.ShowModal()
if dialogResult != wx.ID_OK:
return
path = fileDialog.GetPath()
self.txtStuPath.SetValue(path)
pass
#单击浏览保存目录图片
def OnBtnDesPic(self, event):
dirDesPicPath = wx.DirDialog(self.panel, '选择图片保存目录')
if dirDesPicPath.ShowModal() == wx.ID_OK:
self.txtDesPicPath.SetValue(dirDesPicPath.GetPath())
dirDesPicPath.Destroy()
pass
def OnBtnBeginCreate(self, event):
msg = ''
#msg = msg.replace("\\", "\\\\")
#验证
if not os.path.exists(self.txtSrcPicPath.GetValue()):
msg = msg + '请选择源图片目录\n'
if not os.path.exists(self.txtStuPath.GetValue()):
msg = msg + '请选择学生名单\n'
if not os.path.exists(self.txtDesPicPath.GetValue()):
msg = msg + '请选择保存图片目录\n'
if not msg=='':
#msgDlg = wx.MessageDialog(self.panel, msg, '提示', wx.OK)
#msgDlg.ShowModal()
#msgDlg.Destroy()
pass
#打开excel文件
xlsApp = win32com.client.Dispatch('Excel.Application')
xlsBook = xlsApp.Workbooks.Open(self.txtStuPath.GetValue())
xlsSheet = xlsBook.Sheets[0]
rowCount = xlsSheet.UsedRange.currentregion.Rows.Count#行数
colCount = xlsSheet.UsedRange.currentregion.Columns.Count#列数
#(1,1)->(rowcount, colcount)获取结果集
result = xlsSheet.Range(xlsSheet.Cells(1,1), xlsSheet.Cells(rowCount, colCount)).Value
msg = ''
#获取班级
clsList = []
for row in result[1:len(result)]:
i = 0
for rowCls in clsList:
if row[4]==rowCls:
break;
i = i + 1
if i>=len(clsList):
clsList.append(row[4])
pass
#获取切割块名称
srcPicList = os.listdir(self.txtSrcPicPath.GetValue())
cutBlockList = os.listdir(self.txtSrcPicPath.GetValue()+'\\'+srcPicList[0])
#生成目录
self.txtResult.AppendText('***开始生成班级目录***\n\n')
desPath = self.txtDesPicPath.GetValue()
for row in clsList:
clsPath = desPath + '\\' + row
#先删后建
if os.path.exists(clsPath):
shutil.rmtree(clsPath)
os.makedirs(clsPath)
#创建以切割块命名的子文件夹
for cutRow in cutBlockList:
os.makedirs(clsPath+'\\'+cutRow)
else:
os.makedirs(clsPath)
#创建以切割块命名的子文件夹
for cutRow in cutBlockList:
os.makedirs(clsPath+'\\'+cutRow)
self.txtResult.AppendText('成功创建目录: '+row+'\n ')
self.txtResult.AppendText('\n\n***班级目录创建结束***\n\n')
#开始拷贝文件
self.txtResult.AppendText('\n\n***开始拷贝文件***\n\n请稍后...\n\n')
i = 0
msg = ''
for row in result[1:len(result)]:
#以块文件夹为单位进行拷贝
for cutRow in cutBlockList:
srcPath = self.txtSrcPicPath.GetValue()+'\\'+ row[0]+'\\'+cutRow
desPath = self.txtDesPicPath.GetValue() + '\\' + row[4] + '\\'+cutRow+'\\' + row[1]+'.jpg'
if os.path.exists(srcPath):
i = i + 1
shutil.copyfile(srcPath, desPath)
self.txtResult.AppendText(str(i)+'成功拷贝: '+row[1]+'.jpg\n')
#msg = msg + str(i)+'成功拷贝: '+row[1]+'.jpg\n'
#self.txtResult.AppendText(msg)
self.txtResult.AppendText('\n\n***拷贝图片结束***\n\n')
#释放资源
xlsBook.Close()
del xlsApp
self.txtResult.AppendText(msg)
pass
def main():
app = wx.App(redirect=False)
mainFrame = MainFrame(None, -1)
app.MainLoop()
if __name__ == '__main__':
main()