【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   

总结

分享

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

相关推荐
应用市场26 分钟前
构建自定义命令行工具 - 打造专属指令体
开发语言·windows·python
东方佑28 分钟前
从字符串中提取重复子串的Python算法解析
windows·python·算法
Dfreedom.1 小时前
一文掌握Python四大核心数据结构:变量、结构体、类与枚举
开发语言·数据结构·python·变量·数据类型
一半烟火以谋生1 小时前
Python + Pytest + Allure 自动化测试报告教程
开发语言·python·pytest
虚行1 小时前
C#上位机工程师技能清单文档
开发语言·c#
小羊在睡觉2 小时前
golang定时器
开发语言·后端·golang
CoderCodingNo2 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
叶子丶苏2 小时前
第八节_PySide6基本窗口控件_按钮类控件(QAbstractButton)
python·pyqt
Larry_Yanan2 小时前
QML学习笔记(四十四)QML与C++交互:对QML对象设置objectName
开发语言·c++·笔记·qt·学习·ui·交互
百锦再3 小时前
对前后端分离与前后端不分离(通常指服务端渲染)的架构进行全方位的对比分析
java·开发语言·python·架构·eclipse·php·maven