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)
txt转excel
最新推荐文章于 2024-10-10 17:18:06 发布