txt 文件转 excel

import os.path
import os
import xlwt

path = “D:\work” # 输入路径
pathe = “D:\work” # 输出路径

def txt2xls(fp, file):

if os.path.exists(fp):
    print("正在处理", fp)
    f = open(fp,encoding='utf-8')
    wb = xlwt.Workbook()
    ws1 = wb.add_sheet("Sheet1")
	
    i = 0
    for line in f.readlines():
        j = 0
        for item in line.split('\t'):
            item = item.strip()
            item_list = item.split("|")
            print(item_list)
            for items in item_list:
                ws1.write(i, j, items)
                j = j + 1
        i = i + 1
    f.close()
    wb.save(pathe + "\\" + str(file[:-4]) + '.xls')

def getfiles():

files = os.listdir(path)
for file in files:
    fp = path + "\\" + file
    #print(fp)
    if fp[-3:] == "txt":
        txt2xls(fp, file)

if name == ‘main’:

getfiles()
print("处理完毕,按回车键结束程序。")