Python学习(六):txt分割为xlsx

# 首先安装读写xlsx包 pip install openpyxl==3.0.0 rem 读写 xlsx 3.0.2可能是出现了bug
# -*- coding: utf-8 -*-
import os
import datetime
from openpyxl import Workbook

outpath = r'D:\\txt2xlsx\\'
txtname = 'xls.txt'

# 写入测试文件
def write_txt():
    file = outpath + txtname
    write_path = open(file, 'w')
    for num in range(1, 2000000):  # 迭代 1 到 2000000 之间的数字
        # 拼接txt行 制表符分割,也可以改为逗号分割
        line = '测试'+str(num)+'\t'+datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S')
        # 写入txt行
        write_path.write(line + '\n')
    write_path.close()


# 分割成n个xlsx
def cut_xlsx():
    # 每个文件行数
    LIMIT = 1000000
    # 分割文件数
    file_count = 1
    # 行数组
    data_list = []

    file = outpath + txtname
    if os.path.exists(file):
        with open(file) as f:
            for line in f:
                data_list.append(line[:-1])
                if len(data_list) < LIMIT:
                    continue
                # 数据达到LIMIT
                wb = Workbook()
                ws = wb.active
                for line in data_list:
                    ws.append(line.split('\t')) #制表符分割,也可以改为逗号分割
                wb.save(outpath + str(file_count) + '.xlsx')

                data_list = []
                file_count += 1
        f.close()

        if data_list:
            wb = Workbook()
            ws = wb.active
            for line in data_list:
                ws.append(line.split('\t')) #制表符分割,也可以改为逗号分割
            wb.save(outpath + str(file_count) + '.xlsx')

    print('分割为' + str(file_count)+ '个xlsx文件')


# 程序入口
if __name__ == '__main__':
    cut_xlsx()

 

posted @ 2020-03-19 13:45  liw66  阅读(477)  评论(0编辑  收藏  举报