Mady
Mady

Reputation: 473

write list of list to xlsx and start at row number xx

I have lists within a list, which i want to write into an xlsx file. The tricky thing is that i want it to start writing at row number 9, because there is other stuff before.

the code i tried is below. it only returns a lot of '5' in the column ... i would be really thankful for some help.

list_thing= [['a','b',''], ['1','2','3']]

row_start = 9
for i, l in enumerate(list_thing):
    for j, col in enumerate(l):
        worksheet.write(row_start,i, j, col)

Upvotes: 1

Views: 296

Answers (1)

Since you are wanting append data to an existing Excel document, you may want to consider using openpyxl as xlsxwriter cannot modify an existing Excel file. Here's how you might do it in openpyxl:

import openpyxl

wb = openpyxl.load_workbook(filename = 'Doc.xlsx')

ws = wb['Sheet1']

list_thing = [['a','b',''], ['1','2','3']]

row_start = 9

for row in range(len(list_thing)):
    for col in range(len(list_thing[row])):
        ws.cell(column=col+1, row=row_start, value="{0}".format(list_thing[row][col]))
    row_start+=1

wb.save('Doc.xlsx')

Upvotes: 2

Related Questions