当前位置:   article > 正文

【Python实战】-- 按条件提取所有目录下所有Excel文件指定行数据_python怎么遍历文件夹中所有excle取出特定数值

python怎么遍历文件夹中所有excle取出特定数值

系列文章目录


前言

一、背景

有多个目录,每个目录下有若干Excel文件,我们要提取每个Excel里面指定的行数据:

目录如下:
注:目录数量、名称不限,其中文件数量、名称不限
请添加图片描述

二、使用步骤

1.源码

解释:
每个文件中第三列中若含有关键词“L<1.2”,则将该行数据提取汇总至新表
将源码放置所汇总的目录下即可

#xlwt只支持xls格式,xlsx格式需要用openpyxl或pandas
# coding:utf-8
import pandas as pd
import os 
import xlrd
import xlwt
from xlutils.copy import copy
from openpyxl import workbook
from openpyxl import load_workbook
# 读写2007 excel
import openpyxl
 

def get_allfile_msg(file_dir):
    for root, dirs, files in os.walk(file_dir):
        return root, dirs, [file for file in files if file.endswith('.xls') or file.endswith('.xlsx')]

def get_allfile_url(root, files):
    allFile_url = []
    for file_name in files:
        file_url = root + "/" + file_name
        allFile_url.append(file_url)
    return allFile_url

def get_file_name(path, suffix = ['.xlsx', '.xls']):
    tmp_lst = []
    for root,dirs,files in os.walk(path):
        for file in files:
            tmp_lst.append(os.path.join(root, file))
    return tmp_lst

 
if __name__ == '__main__':
    file_dir = os.getcwd()
    root, dirs, files = get_allfile_msg(file_dir)
    allFile_url = get_allfile_url(root, files)
    print(root)
    print(dirs)
    number = len(dirs)
    print(number)
    n = 0
    #**********************************************************
    jieguo = xlwt.Workbook(encoding="ascii")  #生成excel
    wsheet = jieguo.add_sheet('sheet name') #生成sheet    
    y=0 #生成的excel的行计数
    keyword = 'L<1.2'
    #**********************************************************
    for n in range(len(dirs)):
        dir = dirs[n]
        path = root + '\\' + dir
        print(path) 
        tmp_lst = get_file_name(path)
        print(tmp_lst)
        #main()
        #'''
        try:
            for xl in tmp_lst:
                workbook = xlrd.open_workbook(xl) #读取源excel文件
                print(xl)
                sheetnum=workbook.nsheets  #获取源文件sheet数目
                print(sheetnum)
                #for m in range(0,sheetnum):
                sheet = workbook.sheet_by_index(0) #读取源excel文件第m个sheet的内容
                nrowsnum=sheet.nrows  #获取该sheet的行数
                for i in range(0,nrowsnum):
                    if str(sheet.cell(i,2).value) == keyword:
                        u = i
                        date = sheet.row(u)
                        y = y + 1
                        for j in range(len(date)):
                            wsheet.write(y,j,sheet.cell_value(i,j))
        #jieguo.save('jieguo.xls') #保存新生成的Excel
        except Exception as e:
            print(e)                        
        #jieguo.save('jieguo.xls') #保存新生成的Excel        
        #'''
        n =  n + 1
        y = y + 1
        jieguo.save('jieguo.xls') #保存新生成的Excel   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

总结

分享:

在极端痛苦中,一个灵魂为了承受这份痛苦,将会发出崭新的生命光辉。就是这股潜力在新生命里的发挥,使人们远离在极端痛苦时燃起的自杀念头,让他得以继续活下去。他的心境将别于健康的人,他鄙视世人所认同的价值观,从而发挥昔日所未曾有过的最高贵的爱与情操,这种心境是曾体验过地狱烈火般痛苦的人所独有的。——尼采《曙光》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/768687
推荐阅读
相关标签
  

闽ICP备14008679号