# -*- encoding: utf-8 -*-
import os
import xlwt


def txt_xls(filename, xlsname):
    try:
        f = open(filename)
        xls = xlwt.Workbook()
        # 生成excel的方法,声明excel
        sheet = xls.add_sheet('sheet', cell_overwrite_ok=True)
        x = 0  # 在excel开始写的位置(y)

        while True:  # 循环读取文本里面的内容
            line = f.readline()  # 一行一行的读
            if not line:  # 如果没有内容,则退出循环
                break
            for i in range(len(line.split(' '))):  # \t即tab健分,本处为空格分隔
                item = line.split(' ')[i]
                sheet.write(x, i, item)  # x单元格经度,i单元格纬度
            x += 1  # 另起一行
        f.close()
        xls.save(xlsname)  # 保存为xls文件
    except:
        raise


if __name__ == '__main__':

    # 要提前建好文件夹
    xls_path = '../node_side/fiscobcos/stastic_log/xls/'
    txt_path = '../node_side/fiscobcos/stastic_log/'

    files = os.listdir(txt_path)
    print(files)


    for file_name in files:

        try:


            txt_name = txt_path + file_name
            ## 因为文件名里面包含了文件的后缀,所以重命名的时候要加上
            xls_name = xls_path + file_name.split('.')[0] + '.xls'

            print(txt_name)
            print(xls_name)

            txt_xls(txt_name, xls_name)

        except:
        ## 跳过一些系统隐藏文档
            pass