txt 转成 xlsx

#!/usr/bin/env python3
#========================================================================================================
#   Version : 1.0
#   Date    : 2024-07-15
#   Description: convert txt files to a xlsx 
#======================================================================================================== 
import os
import argparse
from openpyxl import Workbook


def parse_arguments():
    parser = argparse.ArgumentParser(description="covent txt files to a xlxs")
    # 第一列是要填充的文件路径,第二列是 sheet-name
    parser.add_argument("--merge_list_file", "-l", dest="merge_list_file", required=True, help="A multi-line file consisting of sheet_file and sheet_name in 1 line") 
    parser.add_argument("--outfile", "-o", required=True, help="file of xlsx; if outdir not exist, will make it")
    args = parser.parse_args()
    return args


def create_dir(folder):
    if not os.path.exists(folder):
        os.makedirs(folder)


def read_tsv_file(tsv_file):
    """read tsv file to list

    Args:
        merge_file (str): input file with \t sep

    Returns:
        list: [[line_col_1, line_col_2,...],...]
    """    
    out_list = []
    with open(tsv_file) as f:
        for line in f:
            if line == '\n':
                continue
            line_list = line.strip('\n').split('\t')
            out_list.append(line_list)
    return out_list


def add_xlsx(xlsx_file, merge_list_file):
    """读取(sheet_file, sheet_name)的文件,逐条添加到xlsx中

    Args:
        xlsx_file (str): file of xlsx
        merge_list_file (str): A multi-line file consisting of sheet_file and sheet_name in 1 line
    """    
    create_dir(os.path.dirname(xlsx_file))
    merge_list = read_tsv_file(merge_list_file)
    # sheet_file, sheet_name
    wb = Workbook()
    idx = 0
    for (sheet_file, sheet_name) in merge_list:
        ws = wb.create_sheet(index=idx, title=sheet_name)
        idx += 1
        # 读取tsv文件
        file_value_list = read_tsv_file(sheet_file)
        rows = len(file_value_list)
        cols = len(file_value_list[0])
        for row in range(1, rows+1):
            for col in range(1, cols+1):
                value = file_value_list[row-1][col-1]
                ws.cell(column=col, row=row, value=value)
    del wb["Sheet"]
    # print(wb._sheets)
    wb.save(xlsx_file)


def main():
    args = parse_arguments()
    merge_list_file = args.merge_list_file
    outfile = args.outfile
    add_xlsx(outfile, merge_list_file)


if __name__ == "__main__":
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值