Python读取txt内容写入xls格式excel中的方法
由于xlwt目前只支持xls格式,至于xlsx格式,后面会继续更新
import xlwt
import codecs
def Txt_to_Excel(inputTxt,sheetName,start_row,start_col,outputExcel):
fr = codecs.open(inputTxt,'r')
wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet(sheetName)
line_number = 0#记录有多少行,相当于写入excel时的i,
row_excel = start_row
try:
for line in fr :
line_number +=1
row_excel +=1
line = line.strip()
line = line.split(' ')
len_line = len(line)#list中每一行有多少个数,相当于写入excel中的j
col_excel = start_col
for j in range(len_line):
print (line[j])
ws.write(row_excel,col_excel,line[j])
col_excel +=1
wb.save(outputExcel)
except:
print ('')
if __name__=='__main__':
sheetName = 'Sheet2'#需要写入excel中的Sheet2中,可以自己设定
start_row = 7 #从第7行开始写
start_col = 3 #从第3列开始写
inputfile = 'text.txt' #输入文件
outputExcel = 'excel_result.xls' #输出excel文件
Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)el) 相关推荐
dflyzx 2020-09-17
higheels 2020-07-20
Ericbig 2020-07-19
yinyang00 2020-05-15
lijiuchangxin 2020-05-14
yogoma 2020-05-09
jacktangj 2020-04-19
Yyqingmofeige 2020-03-26
idning 2020-02-14
宿舍 2020-01-31
luenxin 2020-01-22
水痕 2020-01-18
chouliqingke 2020-01-08
secondid 2019-12-18
XCodeRush 2019-12-17
ryuhfxz 2019-11-20
qonsnow 2014-03-07
coulder 2019-11-17