35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
# -*- encoding: utf-8 -*-
|
||
import os
|
||
import sys
|
||
current_file_path = os.path.split(os.path.realpath(__file__))[0]
|
||
site_packages_path = os.path.abspath(os.path.join(current_file_path, "../site-packages"))
|
||
sys.path.append(site_packages_path)
|
||
reload(sys)
|
||
|
||
from openpyxl import Workbook
|
||
def txt_xls(filename,xlsname):
|
||
"""
|
||
:文本转换成xls的函数
|
||
:param filename txt文本文件名称、
|
||
:param xlsname 表示转换后的excel文件名
|
||
"""
|
||
try:
|
||
f = open(filename)
|
||
xls= Workbook()
|
||
#生成excel的方法,声明excel
|
||
sheet = xls.create_sheet('sheet1', 0)
|
||
while True:
|
||
#按行循环,读取文本文件
|
||
line = f.readline()
|
||
if not line:
|
||
break #如果没有内容,则退出循环
|
||
sheet.append(line.split('\t'))
|
||
f.close()
|
||
xls.save(xlsname) #保存xls文件
|
||
except:
|
||
raise
|
||
if __name__ == "__main__" :
|
||
av = sys.argv
|
||
filename = av[1]
|
||
xlsname = av[2]
|
||
txt_xls(filename,xlsname) |