【python实战】--提取所有目录下所有Excel文件指定列数据

系列文章目录

文章目录


前言

一、问题描述

需要提取指定路径下所有excel文件中指定一列数据,汇总到新文件,(逐列汇总)

二、python代码

1.引入库

代码如下(示例):

python 复制代码
#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()
    file_dir = r"E:\py\python3.7\test-advance\test04\data"
    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的行数
                ncolsnum=sheet.ncols  #获取该sheet的列数

                date = sheet.col(3)
                y = y + 1 
                for j in range(len(date)):
                    wsheet.write(j,y,sheet.cell_value(j,3))

        #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   

总结

分享

如果我们有意无意地卷入某种旋涡,那我们的大脑很快就会塞满乱七八糟的东西, 弄得我们头昏眼花,心乱如麻,我们的身心会很累。

相关推荐
有个傻瓜3 分钟前
PHP语言核心技术全景解析
开发语言·kubernetes·php
Amo Xiang22 分钟前
《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
python·选择题·基础语法
菥菥爱嘻嘻26 分钟前
JS手写代码篇---手写ajax
开发语言·javascript·ajax
江梦寻32 分钟前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
霖檬ing35 分钟前
Python——MySQL远程控制
开发语言·python·mysql
miniwa38 分钟前
Python编程精进:CSV 模块
python
qq_429879672 小时前
省略号和可变参数模板
开发语言·c++·算法
优秀的颜3 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
CodeWithMe3 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi3 小时前
【QT控件】输入类控件详解
开发语言·c++·qt