Find_Excel_From_Dir获取特定目录下的excel表格,将数据复制出来

  1 # -*- coding: utf-8 -*-
  2 # -主要思路-,获取解压后的日志文件包
  3 # -获取特定目录下的excel表格,将数据复制出来
  4 import  xdrlib ,sys
  5 import xlrd
  6 import os
  7 import time
  8 class Search_Excel_From_Dir:
  9     #file_name = "example"#此处确定要搜寻的文件名字
 10     Bdc_Csv_list = []#Excel文件列表
 11     Bdc_Csv_Dir_list = []#Excel文件所在目录的列表
 12     #此处自定义函数判断文件类型是不是表格类型
 13     def IsExcel(self,file):
 14         compress = [".xlsx",".csv"]
 15         for z in compress:
 16             if file.endswith(z):#描述:判断字符串是否以指定字符或子字符串结尾.
 17                 return True
 18         return False
 19 
 20     #判断是否是ubp_bdc_01表格文件
 21     def IsBdcCsvFile(self, fileName):
 22         [dirname, filename] = os.path.split(fileName)
 23         BdcFiles = set(["ubp_bdc01_info"])
 24         for nameFile in BdcFiles:
 25             if nameFile in filename:
 26                 return True
 27             else:
 28                 continue
 29         return False
 30     
 31     #递归查看某个文件夹下面的所有Excel文件
 32     def FindBDCCsvFile(self, fileDirPath):
 33         fileNames = os.listdir(fileDirPath)
 34         for file in fileNames:
 35             filePath = str(fileDirPath) + "\" + str(file)
 36             if os.path.isdir(filePath):
 37                 self.FindBDCCsvFile(filePath)
 38             elif os.path.isfile(filePath):
 39                 #此处判断是否是BDC日志巡检所需的文件,同时判断文件类型是否是Excel类型
 40                 if self.IsBdcCsvFile(filePath) == True and self.IsExcel(filePath) == True:
 41                     #此处添加Excel文件的绝对路径名到文件名列表里面去
 42                     self.Bdc_Csv_list.append(filePath)
 43                     [dirname, filename] = os.path.split(filePath)
 44                     #此处添加Excel文件夹的路径到文件夹路径列表里面去
 45                     self.Bdc_Csv_Dir_list.append(dirname)
 46                 else:
 47                     continue
 48             else:
 49                 continue
 50 
 51     #先获取特定目录下的excel表格:
 52     #1.进入指定目录
 53     #2.获取excel文件
 54     #3.打印所有同目录下的文件
 55     def Find_Excel(self,filePath):
 56         [dirname, filename] = os.path.split(filePath)
 57         os.chdir(dirname)#os.chdir()方法用于改变当前工作目录到指定的路径,路径是"D:日志巡检存放路径"
 58         path = os.getcwd()#os.getcwd() 方法用于返回当前工作目录,工作目录为"D:日志巡检存放路径"
 59         file_names = os.listdir("./")#os.listdir()方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。
 60                                      #它不包括 '.' 和'..' 即使它在文件夹中。只支持在Unix,Windows下使用。
 61         for i in range(len(file_names)):
 62             if self.IsBdcCsvFile(file_names[i]) == True:
 63                print(file_names[i])
 64        
 65     def Run(self):
 66         self.FindBDCCsvFile("D:广东应急厅巡检日志")
 67         for i in range(0,len(self.Bdc_Csv_list)):
 68             #self.open_excel(self.Bdc_Csv_list[i])
 69             #print(self.Bdc_Csv_list[i])
 70             self.Find_Excel(self.Bdc_Csv_list[i])
 71             return self.Bdc_Csv_list
 72             #print(self.Bdc_Csv_Dir_list[i])
 73 
 74 '''       
 75     #打开excel文件
 76     def open_excel(self,file_csv):
 77         try:
 78             data = xlrd.open_workbook(file_csv)
 79             return data
 80         except IOError:
 81             print("check the file's name")
 82     
 83     #根据名称获取Excel表格中的数据   参数:file:Excel文件路径     colnameindex:表头列名所在行的索引  ,by_name:Sheet1名称
 84     def excel_table_byname(file= 'example.xlsx', colnameindex=0, by_name=u'Sheet1'):
 85         data = open_excel(file) #打开excel文件
 86         table = data.sheet_by_name(by_name) #根据sheet名字来获取excel中的sheet
 87         nrows = table.nrows #行数
 88         colnames = table.row_values(colnameindex) #某一行数据
 89         list =[] #装读取结果的序列
 90         for rownum in range(0, nrows): #遍历每一行的内容
 91             row = table.row_values(rownum) #根据行号获取行
 92             if row: #如果行存在
 93                 app = [] #一行的内容
 94                 for i in range(len(colnames)): #一列列地读取行的内容
 95                     app.append(row[i])
 96                 list.append(app) #装载数据
 97         #print(list)
 98         return list
 99 '''
100     
101 
102     #'ubp_bdc01_info.csv'
原文地址:https://www.cnblogs.com/dog-and-cat/p/11613817.html