python书写两个excel合并

左心房为你撑大大i 提交于 2020-02-12 04:02:03
# -*- 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)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!