excel内容如上图, 如果state等于1,则复制对应的资源,如果state等于0,则不复制。
python代码如下:
import xlrd
import os
from shutil import copy
# 为了去除excel表中的双引号【因为个人需要,excel表中必须添加双引号】
def changeExcelStringFormat(str):
return str[1:-1]
def copyResources(excelName):
#读图excel
data = xlrd.open_workbook(excelName)
tData = data.sheets()[0]
nrows = tData.nrows
rootPath = os.path.dirname(os.path.realpath(__file__)) #python文件的路径
resourcesPath = "F:\\python\\excelAndCopyTest\\" #资源路径
copyPath = rootPath + "\\..\\copyTest\\"\ #目标路径
for i in range(nrows):
data = tData.row(i)[5].value
if data == "1" :
filePath = changeExcelStringFormat(tData.row(i)[3].value) #从excel中读取文件路径
fileName = changeExcelStringFormat(tData.row(i)[4].value) #从excel中读取文件名
srcPath = resourcesPath + filePath + fileName
destPath = copyPath + filePath\
if not os.path.isdir(destPath): #看看目标路径是否存在,若不存在则创建对应目录
os.makedirs(destPath)
copy(srcPath, destPath) #复制资源
copyResources("fileCopy.xlsx")
中文注释为后期加工,若代码不能运行,则删除所有中文注释