Home > OS >  appending lists to an existing excel file starting from a specific cells with Python, Openpyxl
appending lists to an existing excel file starting from a specific cells with Python, Openpyxl

Time:01-16

I'm getting no of list's in my_list and want to add list's item in excel from cell E3 to length of my_list cell cell (AI8)

print(my_list)
['W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue']
['Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue']
['Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W']
['Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W']
['Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue']

And I want add like this image replace other items like mon, tue, wed with None and add just W

enter image description here

My Code is

for r in range(4, 9):
    for c,val in zip(5, my_list):
        ws.cell(row=r, column=c).value=val


CodePudding user response:

Just iterate through the Excel range by cell, and use the cell positon to find the value in the list then write to the cell if the value returned from the list is 'W'.
Adjust the range max/min rows to suit the actual rows.

import openpyxl

my_list = [['W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon',
            'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'W', 'Mon', 'Tue'],
           ['Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W',
            'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'W', 'Tue'],
           ['Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon',
            'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W'],
           ['Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon',
            'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'W'],
           ['Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon',
            'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'W', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue']]

filename = 'foo.xlsx'

wb = openpyxl.load_workbook(filename)
ws = wb['Sheet1']

start_column = 5
for enum, row in enumerate(ws.iter_rows(min_col=start_column, max_col=35, min_row=4, max_row=8)):
    for cell in row:
        listval = my_list[enum][cell.column - start_column]
        if listval == 'W':
            cell.value = listval

wb.save('foo_out.xlsx')
  • Related