【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   

总结

分享

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

相关推荐
小雅痞7 分钟前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
We་ct10 分钟前
AI辅助开发术语体系深度剖析
开发语言·前端·人工智能·ai·ai编程
t***54411 分钟前
Dev-C++中哪些选项可以设置
开发语言·c++
FreakStudio15 分钟前
MicroPython对接大模型:uopenai + 火山方舟实现文字聊天和图片理解
python·单片机·ai·嵌入式·面向对象·电子diy
Kimliao16618 分钟前
TFT-LCD液晶显示模组常见驱动电压的作用
python
輕華20 分钟前
PyQt5入门实战:安装、QtDesigner设计与PyUIC转换完整指南
开发语言·qt
qq_2069013921 分钟前
CSS如何引入自适应图标_利用svg外链配合css控制颜色
jvm·数据库·python
weixin_4087177728 分钟前
Go语言怎么编译Linux程序_Go语言编译Linux可执行文件教程【避坑】
jvm·数据库·python
麻辣璐璐37 分钟前
EditText属性运用之适配RTL语言和LTR语言的输入习惯
android·xml·java·开发语言·安卓
2501_944934731 小时前
财务岗位如何在工作中提升数据分析能力?从财务分析到经营分析这样练
python·信息可视化·数据分析