python:使用openpyxl保存列表列表?

问题描述 投票:0回答:1

我下面有一个列表列表:

[['Module', 'Function'],
 ['s', 'b', 'mc'],
 ['TC1', 'TC2', 'TC3', 'TC4'],
 ['status 1', 'status 2' , 'status 3', 'status 4']]

并且我想使用openpyxl库将数据保存在Excel工作表中。

enter image description here

您能帮我用这种格式保存吗?

python openpyxl
1个回答
0
投票

这就是我所做的,直到现在。

from openpyxl import Workbook
wb=Workbook()
sheet = wb.active

first_row = [('Module column ','function column','TC Name Column', 'TC status  Column')]

for row in first_row: 
    sheet.append(row)

nombre_TC = len(result[2])

print(nombre_TC)
start = 2
sheet.merge_cells(start_row = start, start_column = 1, end_row = start +nombre_TC - 1, end_column = 1)
sheet.merge_cells(start_row = start, start_column = 2, end_row = start +nombre_TC - 1, end_column = 2)




sheet.cell(row = start, column = 1, value = result[0][0])
sheet.cell(row = start, column = 2, value = result[0][1])

TC = result[2]
for elt in TC:
    sheet.cell(row = start, column = 3, value = elt)
    start = start + 1
start_status = 2    
status = result[3]
for elt in status:
    sheet.cell(row = start_status, column = 4, value = elt)
    start_status = start_status + 1


wb.save(filepath)

它可以工作,但我认为还没有优化

© www.soinside.com 2019 - 2024. All rights reserved.