需要提前安装相关python包(如果通过安装anconda可以忽略)
pip3 xlwt
pip3 codecs
python代码
import xlwt
import codecs
#输入的文本文件的路径
input_txt = r'D:\Users\test0.txt'
#输出excel的路径
output_excel = r'D:\Users\finish0.xls'
#保存到excel的那个工作表
sheetName = 'test'
#从哪行哪列开始
start_row = 0
start_col = 0
wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet(sheetName)
f = open(input_txt, encoding = 'utf-8')
row_excel = start_row
for line in f:
line = line.strip('\n')
line = line.split(' ')
print(line)
col_excel = start_col
len_line = len(line)
for j in range(len_line):
print (line[j])
ws.write(row_excel,col_excel,line[j])
col_excel += 1
wb.save(output_excel)
row_excel += 1
f.close