全文使用的是xlswriter模块写的,也有人使用 xlrd与 xlutils模块实现,不过还未进行验证
import xlsxwriter
workbook = xlsxwriter.Workbook("D:\\Program Files\\subpy\\sql2.xlsx")#在指定目录下创建一个excle
worksheet = workbook.add_worksheet("students")#新建一个sheet
title_index = ["A","B","C","D"]#sheet中的区域
li = [] #定义一个空列表
blod = workbook.add_format({"bold":True})#定义exlce中写入的字体with open("D:\\Program Files\\subpy\\tets.txt",'r') as f1:#打开txt文档
lines = f1.readlines()#读取所有行内容
n = -1#定义一个变量
for x in lines:#逐行读取
n=n+1
li.append(x[:-1])#去掉回车符
y= x.split#以空格分字符
for i in range(len(title_index)):#读取excle区域下标
# for i,j in enumerate(title_index):
content = y[i]#单个字符读取
worksheet.write(n,i,content,blod)#分行分列写入workbook.
close#关闭excle