pytho-多个excel合并

本文介绍了一种使用Python批量读取并处理多个Excel文件的方法,通过xlrd和xlwings库实现数据的读取和写入,适用于需要对大量Excel文件进行统一处理的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: UTF-8 -*-

import os
import xlrd
import xlwt
import xlwings as xw

root_dir = os.path.abspath('.')
write_excel_path = root_dir + "\\demo.csv"
path = root_dir + "\\test"
write_excel_book = xw.Book()
sht = write_excel_book.sheets('sheet1')
#封装文件名称
L = []
for root, dirs,files in os.walk(path):
    for file in files:
        if os.path.splitext(file)[1] == '.xlsx':  #查询文件后缀
            L.append(os.path.join(root,file))
print(len(L))
#封装excel数据
sheet_col = []
for i in range(len(L)):
    read_excel_path = L[i]
    #打开文件,获取excel文件的workbook对象
    read_excel = xlrd.open_workbook(read_excel_path,encoding_override="UTF-8")
    sheet = read_excel.sheet_by_index(0)
    for i in range(sheet.nrows):#循环打印每一行
        if i==0:
            continue
        else:
            sheet_col.append(sheet.row_values(i))
for each_col in range(len(sheet_col)):
    col = "A" + str(each_col + 1)
    sht.range(col).value = sheet_col[each_col]
write_excel_book.save(write_excel_path)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值