【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   

总结

分享

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

相关推荐
B站_计算机毕业设计之家8 分钟前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
渣渣苏16 分钟前
Langchain实战快速入门
人工智能·python·langchain
3GPP仿真实验室17 分钟前
【Matlab源码】6G候选波形:OFDM-IM 增强仿真平台 DM、CI
开发语言·matlab·ci/cd
devmoon21 分钟前
在 Polkadot 上部署独立区块链Paseo 测试网实战部署指南
开发语言·安全·区块链·polkadot·erc-20·测试网·独立链
lili-felicity21 分钟前
CANN流水线并行推理与资源调度优化
开发语言·人工智能
沐知全栈开发22 分钟前
CSS3 边框:全面解析与实战技巧
开发语言
lili-felicity25 分钟前
CANN模型量化详解:从FP32到INT8的精度与性能平衡
人工智能·python
数据知道27 分钟前
PostgreSQL实战:详解如何用Python优雅地从PG中存取处理JSON
python·postgresql·json
island131432 分钟前
CANN GE(图引擎)深度解析:计算图优化管线、内存静态规划与异构 Stream 调度机制
c语言·开发语言·神经网络
曹牧36 分钟前
Spring Boot:如何在Java Controller中处理POST请求?
java·开发语言