Python将txt转为xlsx文件
# Python将txt转为xlsx文件
使用到的库为openpyxl
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
@version : v1.0
@author : fangzheng
@contact : zfang@hillinsight.com
@software : PyCharm
@filename : txt_xls.py
@create time: 2018/9/11 下午8:07
@describe :
@use example: python txt_xls.py [param1 param2]
-------------------------------------------------
"""
import csv
import openpyxl
import sys
def txt_to_xls(input_file,output_file):
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_file, 'rb') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
ws.append(row)
wb.save(output_file)
if __name__ == '__main__':
if len(sys.argv) < 3:
help_text = """
=====参数个数不够=====
参数1:txt_file_path
参数2:xls_file_path
"""
print help_text
exit(0)
filename = sys.argv[1]
xlsname = sys.argv[2]
txt_to_xls(filename, xlsname)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
最后更新时间: 2022/7/23 10:17:11