浅墨散人 浅墨散人
  • 基础
  • 设计模式
  • JVM
  • Maven
  • SpringBoot
  • 基础
  • Flask
  • Diango
  • Pandas
  • SqlAlchemy
  • Sqoop
  • Flume
  • Flink
  • Hadoop
  • Hbase
  • Hive
  • Kafka
  • Kylin
  • Zookeeper
  • Tez
  • MySQL
  • Doris
  • Chrome
  • Eclipse
  • IDEA
  • iTerm2
  • Markdown
  • SublimeText
  • VirtualBox
  • WebStrom
  • Linux
  • Mac
  • Hexo
  • Git
  • Vue
  • VuePress
  • 区块链
  • 金融
数据仓库
数据治理
读书笔记
关于我
GitHub (opens new window)
  • 基础
  • 设计模式
  • JVM
  • Maven
  • SpringBoot
  • 基础
  • Flask
  • Diango
  • Pandas
  • SqlAlchemy
  • Sqoop
  • Flume
  • Flink
  • Hadoop
  • Hbase
  • Hive
  • Kafka
  • Kylin
  • Zookeeper
  • Tez
  • MySQL
  • Doris
  • Chrome
  • Eclipse
  • IDEA
  • iTerm2
  • Markdown
  • SublimeText
  • VirtualBox
  • WebStrom
  • Linux
  • Mac
  • Hexo
  • Git
  • Vue
  • VuePress
  • 区块链
  • 金融
数据仓库
数据治理
读书笔记
关于我
GitHub (opens new window)
  • Python基础

    • Python基础
    • Python基础
    • 在MacOS上安装Python2和Python3
    • 在MacOS上使用python安装pymssql库
    • 使用Python构建交互式的Dashboard
    • Python一行代码实现文件共享服务器
    • Python浏览器自动化工具splinter
    • Python将txt转为xlsx文件
      • Python将txt转为xlsx文件
    • Python的virtualenv虚拟环境
  • Python
  • Basic
2018-09-19
目录

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
#Python#openpyxl
最后更新时间: 2022/7/23 10:17:11
Python浏览器自动化工具splinter
Python的virtualenv虚拟环境

← Python浏览器自动化工具splinter Python的virtualenv虚拟环境→

最近更新
01
分区分桶
08-21
02
数据模型(重要)
08-21
03
安装和编译
08-21
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式