写入excel—xlsxwriter使用

import xlsxwriter


def make_excel(file_name, excel_data):
    workbook = xlsxwriter.Workbook(file_name)
    worksheet = workbook.add_worksheet("Sheet1")
    set_worksheet = workbook.get_worksheet_by_name("Sheet1")
    set_worksheet.set_column("A:G", 20)
    for index in range(len(excel_data)):
        worksheet.write_row(index, 0, excel_data[index])
    workbook.close()


if __name__ == '__main__':
    excel_data = [
        ["IP地址", "MAC地址", "接口", "设备管理IP"],
        ["192.168.1.1", "B7:E3:B1:58:92:B1", "MEth0/0/1", "172.27.55.22"],
        ["192.168.1.2", "B8:E3:B1:58:92:B2", "MEth0/0/2", "172.27.55.23"],
        ["192.168.1.3", "B9:E3:B1:58:92:B3", "MEth0/0/1", "172.27.55.24"]
    ]
    file_name = r"E:\My_Work\TEST\test.xlsx"

    make_excel(file_name, excel_data)

 

posted @ 2022-10-06 19:41  TianShu  Views(140)  Comments(0Edit  收藏  举报