吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3465|回复: 10
收起左侧

[Python 原创] 【Python】用於純文本(例如txt)轉excel

[复制链接]
Chost 发表于 2019-5-24 16:12
最近在學習python,感覺有、方便好用。
如果代碼你有用到能幫助到你,那麽給個免費評分熱心值(ps: 還沒能進影視區),多謝

---------------------------分隔符---------------------------------------------------------------

[Python] 纯文本查看 复制代码
#!/usr/bin/python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     txt转换成Excel
# Author:      decpc
# Created:     2019-05-24
# update:      2019-05-24
#-------------------------------------------------------------------------------
import datetime
import time
import os
import sys
import xlwt #需要的模块
def parseRec(amt):
        amt_indx = str(amt).index(".")#返回ant字符串以"."結束的索引值
        long_inf = "{:,}".format(long(str(amt)[0:amt_indx]))#千分位隔开
        del_inf = str(amt)[amt_indx+1:]
        del_len = len(del_inf.strip())#strip() 方法用于移除字符串头尾指定的字符(默认为空格)
        if del_len == 0:
                ret_amt = "%s.00" %long_inf
                return ret_amt
        else:
                if del_len == 1:
                        ret_amt = "%s.%s0" %(long_inf,del_inf)
                        return ret_amt
                else:
                        ret_amt = "%s.%s" %(long_inf,del_inf)
                        return ret_amt

def txt2xls(filename,xlsname):  #文本转换成xls的函数,filename 表示一个要被转换的txt文本,xlsname 表示转换后的文件名
    print 'converting xls ... '
    f = open(filename)   #打开txt文本进行读取
    x = 1                #在excel开始写的位置(y)
    y = 0                #在excel开始写的位置(x)
    xls=xlwt.Workbook()
    sheet = xls.add_sheet('sheet1',cell_overwrite_ok=True) #生成excel的方法,声明excel
    style = xlwt.XFStyle()#赋值style为XFStyle(),初始化样式
    al = xlwt.Alignment()
    al.horz = 0x02 #設置水平居中
    al.vert = 0x01 #设置垂直居中
    style.alignment = al
    borders = xlwt.Borders()#单元格增加边框
    borders.left = 1
    borders.right = 1
    borders.top = 1
    borders.buttom = 1
    frame = xlwt.XFStyle()
    frame.borders = borders
    sheet.col(0).width = (20*200)#设置单元格宽度和长度
    sheet.col(1).width = (20*200)
    sheet.col(2).width = (20*200)
    sheet.col(3).width = (20*367)
    sheet.col(4).width = (20*100)
    sheet.col(5).width = (20*300)
    sheet.col(6).width = (20*300)
    sheet.col(7).width = (20*300)
    sheet.col(8).width = (20*250)
    sheet.col(9).width = (20*200)
    sheet.col(10).width = (20*200)
    sheet.col(11).width = (20*200)
    sheet.col(12).width = (20*200)
    sheet.write(0,0,'日期'.strip().decode('utf8'))
    sheet.write(0,1,'日期'.strip().decode('utf8'))
    sheet.write(0,2,'日期'.strip().decode('utf8'))
    sheet.write(0,3,'日期'.strip().decode('utf8'))
    sheet.write(0,4,'日期'.strip().decode('utf8'))
    sheet.write(0,5,'日期'.strip().decode('utf8'))
    sheet.write(0,6,'日期'.strip().decode('utf8'))
    sheet.write(0,7,'日期'.strip().decode('utf8'))
    sheet.write(0,8,'日期'.strip().decode('utf8'))
    sheet.write(0,9,'日期'.strip().decode('utf8'))
    sheet.write(0,10,'金額'.strip().decode('utf8'))
    sheet.write(0,11,'金額'.strip().decode('utf8'))
    sheet.write(0,12,'金額'.strip().decode('utf8'))
    while True:  #循环,读取文本里面的所有内容
        line = f.readline() #一行一行读取
        #print(line)
        body_inf = line.split('|')
        if not line:  #如果没有内容,则退出循环
           break
        else:
            for y in range(0,13):
                if y in (10,11,12):
                    item=parseRec(body_inf[y].strip().decode('utf8'))
                    sheet.write(x,y,item)
                else:
                    item=body_inf[y].strip().decode('utf8')
                    sheet.write(x,y,item)
        x += 1 #另起一行
        y = 0  #初始成第一列
    f.close()
    xls.save(xlsname+'.xls') #保存

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

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

水鸟 发表于 2019-5-24 16:25
纯文本 转EXE意义何在,楼主可以详细说明下嘛
马可菠萝蜜 发表于 2019-5-24 16:33
看着不错,楼主写的比起excel自带的有什么优化吗?
 楼主| Chost 发表于 2019-5-24 16:40
比如data你从数据库里exp出来,只是简单的数字文字的文本。这个时候转为excel,第一excel的可观好有标题且便于编辑。第二数据的二次加工,例如金额数据10000可转为千分位$10,000。总之要对数据进行二次加工的(例如报表)都能用到
 楼主| Chost 发表于 2019-5-24 16:46
马可菠萝蜜 发表于 2019-5-24 16:33
看着不错,楼主写的比起excel自带的有什么优化吗?

主要用於脚本的調用
 楼主| Chost 发表于 2019-5-24 16:47
水鸟 发表于 2019-5-24 16:25
纯文本 转EXE意义何在,楼主可以详细说明下嘛

看樓下有回復
yhxt 发表于 2019-5-24 16:54
支持一下,以资鼓励
hellodream 发表于 2019-5-25 09:41
水鸟 发表于 2019-5-24 16:25
纯文本 转EXE意义何在,楼主可以详细说明下嘛

是excel不是EXE。
nizepeng2009 发表于 2019-5-25 12:37
谢谢楼主啦,拿了。
hbkx 发表于 2019-6-13 15:42
谢谢楼主啦
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 09:09

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表