# coding=utf-8
import os
import xlwt
"""
txt数据写入excel,每个txt数据占一行
2018.12.26
"""
l = []
file_path = 'F:/beijing/聊天记录.xls'#要写入的文件
f = xlwt.Workbook()
##在已有excel中插入数据
# f = xlrd.open_workbook(file_path)
# wb = copy(f)
# sheet = wb.get_sheet(0)
sheet = f.add_sheet('sheet1', cell_overwrite_ok=True)
pathDir = os.listdir("F:/beijing/聊天记录") #txt文件放置在文件夹中,用来获取聊天记录文件夹内所有文件目录
i = 1
for s in pathDir:
newDir = os.path.join("F:/beijing/聊天记录/", s) #把获取的文件路径整合,获取文件完整路径
f1 = open(newDir, 'r', encoding='utf-8')
lines = f1.readlines()
string = ""
for line in lines:
string += line
n = str(s).split('.')[0][0]
sheet.write(0, 0, '备注')
sheet.write(i, 0, s)
sheet.write(0, 1, '聊天内容')
sheet.write(i, 1, string)
i = i+1
f.save(file_path)