python写入表格xpython写入表格xlsx

from openpyxl.workbook import Workbook
from openpyxl.styles import Alignment

def read_log(log_apth):
    # 读取log日志放入列表,自行匹配
    pass


def write_to_excel(result_list, xlsx_path):
    outwb = Workbook()
    ws1 = outwb.active
    align = Alignment(horizontal='center', vertical='center', wrap_text=False)

    # 表头
    ws1.cell(1, 1, "第1列名").alignment = align
    ws1.cell(1, 2, "第2列名").alignment = align
    ws1.cell(1, 3, "第3列名").alignment = align
    ws1.cell(1, 4, "第4列名").alignment = align
    ws1.cell(1, 5, "第5列名").alignment = align
    。。。。。。。。。。。

    # 从第二行开始写数据
    row = 2
    for i in range(len(result_list)):
        print("第 %s个:" % (i + 1), result_list[i])
        ws1.cell(row + i, 1, i + 1).alignment = align
        ws1.cell(row + i, 2, result_list[i][0]).alignment = align
        ws1.cell(row + i, 3, result_list[i][1]).alignment = align
        ws1.cell(row + i, 4, result_list[i][2]).alignment = align
        ws1.cell(row + i, 5, result_list[i][3]).alignment = align

    outwb.save(xlsx_path)

#### log文件存在的最外层目录,两层目录
log_base_path = "log"
##### 表格存放的目录,在写入时,会按照log目录的形式往下创建文件夹以及创建和log同名的表格
xlsx_base_path = "log_xlsx"
def batch_write():
    for first_log_name in os.listdir(log_base_path):
        log_path_list = os.listdir(os.path.join(log_base_path, first_log_name))
        fist_xlsx_path = os.path.join(xlsx_base_path, first_log_name)
        if not os.path.exists(fist_xlsx_path):
            os.mkdir(fist_xlsx_path)
        for log_name in log_path_list:
            end_log_path = os.path.join(log_base_path, first_log_name, log_name)
            xlsx_path = os.path.join(fist_xlsx_path, log_name.rsplit(".")[0] + ".xlsx")
            result_list = read_log(end_log_path)
            write_to_excel(result_list, xlsx_path)

batch_write()
``
发布了52 篇原创文章 · 获赞 8 · 访问量 6040
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览