# -*- coding:utf-8 -*-

import xlrd
import xlsxwriter
import os
import sys
#读取目录 遍历文件 读取每一个文件的第一个sheet 写入目标文件 放在一个sheet里



def open_xls(file):
    try:
        fh = xlrd.open_workbook(file)
        return fh
    except Exception as e:
        print("打开文件错误:" + e)


# 根据excel名以及第几个标签信息就可以得到具体标签的内容
def get_file_value(filename, sheetnum):
    rvalue = []
    fh = open_xls(filename)
    sheet = fh.sheets()[sheetnum]
    row_num = sheet.nrows
    for rownum in range(0, row_num):
        rvalue.append(sheet.row_values(rownum))
    return rvalue

end_xls = "L:\\excel\\final_excel.xlsx"

path = "L:\\excel\\".decode('utf-8').encode('GB2312')
filelist = os.listdir(path)

# 定义一个目标excel
endxls = xlsxwriter.Workbook(end_xls)
end_xls_sheet = endxls.add_worksheet('aa')
num = 1
for files in filelist:
    fh = open_xls(path + files)
    sheet = fh.sheets()[0]
    row_num = sheet.nrows
    for rownum in range(1, row_num):
        #print sheet.row_values(rownum)
        end_xls_sheet.write_row('A' + str(num), sheet.row_values(rownum))
        num += 1
endxls.close()

标签: none

添加新评论