txt转excel

import openpyxl
from openpyxl.utils import get_column_letter

read_file = 'txt1.txt'

# 跳过指定行
skip_line = 0
# 偏移
padding_x = 100
padding_y = 100

# 保存文件
save_file = 'excel文件.xlsx'

# 创建excel
xls = openpyxl.Workbook()
sheet = xls.active
title2 = None
with open(read_file, 'r') as fp:
    lines = fp.readlines()
    tmp_y = padding_y
    for idx, line in enumerate(lines):
        if idx < skip_line:
            continue
        line = [x for x in line.strip()]
        if title2 is None:
            title2 = ['']
            title2.extend([padding_x + x for x in range(len(line))])
            sheet.append(title2)
        line.insert(0, tmp_y)
        tmp_y += 1
        sheet.append(line)

for i in range(1, sheet.max_column + 1):
    sheet.column_dimensions[get_column_letter(i)].width = 2.5

xls.save(save_file)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值