zhoujinyi

  把txt文本转换成xls的Excel的脚本:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     txt转换成Excel
# Author:      zhoujy
# Created:     2013-05-07
# update:      2013-05-07
#-------------------------------------------------------------------------------
import datetime
import time
import os
import sys
import xlwt #需要的模块

def txt2xls(filename,xlsname):  #文本转换成xls的函数,filename 表示一个要被转换的txt文本,xlsname 表示转换后的文件名
    print \'converting xls ... \'
    f = open(filename)   #打开txt文本进行读取
    x = 0                #在excel开始写的位置(y)
    y = 0                #在excel开始写的位置(x)
    xls=xlwt.Workbook()
    sheet = xls.add_sheet(\'sheet1\',cell_overwrite_ok=True) #生成excel的方法,声明excel
    while True:  #循环,读取文本里面的所有内容
        line = f.readline() #一行一行读取
        if not line:  #如果没有内容,则退出循环
            break
        for i in line.split(\'\t\'):#读取出相应的内容写到x
            item=i.strip().decode(\'utf8\')
            sheet.write(x,y,item)
            y += 1 #另起一列
        x += 1 #另起一行
        y = 0  #初始成第一列
    f.close()
    xls.save(xlsname+\'.xls\') #保存

if __name__ == "__main__":
    filename = sys.argv[1]
    xlsname  = sys.argv[2]
    txt2xls(filename,xlsname)

使用方法:

zhoujy@zhoujy:~$ python t2e.py /home/zhoujy/outfile/out.txt  ABC

效果:

 

其他:一些基本的操作请见这里

分类:

技术点:

相关文章:

  • 2021-12-26
  • 2021-12-28
  • 2021-11-19
  • 2021-09-07
  • 2021-12-28
  • 2021-12-28
  • 2021-12-05
  • 2021-12-03
猜你喜欢
  • 2021-10-16
  • 2021-12-28
  • 2020-07-23
  • 2021-06-27
  • 2022-02-15
  • 2021-11-23
相关资源
相似解决方案