美文网首页
Python纯文本导出Excel表格

Python纯文本导出Excel表格

作者: Junetaurus | 来源:发表于2017-12-05 15:49 被阅读0次

纯文本文件student.txt写到student.xls文件中。

student.txt结构:
#-*-coding: utf-8-*-

import xlwt

with open('student.txt', 'r', encoding='utf-8') as f:
    data = f.read()
    _student = eval(data)
    student = list()
    for i in range(1, 4):
        info = _student[str(i)]
        student.append(i)
        student.extend(info)
    row = len(_student)/len(student)


def horz_left(x, y, data):
    algnt = xlwt.Alignment()
    algnt.horz = xlwt.Alignment.HORZ_LEFT
    style = xlwt.XFStyle()
    style.alignment = algnt
    table.write(x, y, data, style)

file = xlwt.Workbook()
table = file.add_sheet('student')
for i in range(len(student)):
    if not i % 5:
        horz_left(i//5, i % 5, student[i])
    else:
        table.write(i//5, i % 5, student[i])

file.save('student.xls')
运行结果student.xls:

相关文章

网友评论

      本文标题:Python纯文本导出Excel表格

      本文链接:https://www.haomeiwen.com/subject/qnjcixtx.html