1

I'm attempting to have list items stored in a variable populate cells of a spreadsheet until the list runs out of items (so it works on a list of any size). I have tried a variety of for loops and slices with no luck.

import openpyxl
wb = openpyxl.Workbook()

sheet=wb.active

addresses=['132 W Raven Dr, Chandler, AZ 85286', '2412 W Binner Dr, Chandler, AZ 85224', '1081 W Lark Dr, Chandler, AZ 85286', '1603 W Maplewood St, Chandler, AZ 85286', '1300 W Estrella Dr, Chandler, AZ 85224', '2120 E Geronimo St, Chandler, AZ 85225', '2100 W Shannon St, Chandler, AZ 85224', '4591 S Felix Pl, Chandler, AZ 85248', '2290 E Cherrywood Pl, Chandler, AZ 85249', '23421 S 130th St, Chandler, AZ 85249', '1601 E Shannon St, Chandler, AZ 85225']

sheet['A1':] = addresses[0:]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
Max Maher
  • 13
  • 2

1 Answers1

2

What you really want to do is add each element of the list into it's own cell as in A1, A2, A3 and so on. Try it like this and see if that gives you the results that you're looking for.

import openpyxl
wb = openpyxl.Workbook ()

sheet = wb.active

addresses = [
    '132 W Raven Dr, Chandler, AZ 85286',
    '2412 W Binner Dr, Chandler, AZ 85224',
    '1081 W Lark Dr, Chandler, AZ 85286',
    '1603 W Maplewood St, Chandler, AZ 85286',
    '1300 W Estrella Dr, Chandler, AZ 85224',
    '2120 E Geronimo St, Chandler, AZ 85225',
    '2100 W Shannon St, Chandler, AZ 85224',
    '4591 S Felix Pl, Chandler, AZ 85248',
    '2290 E Cherrywood Pl, Chandler, AZ 85249',
    '23421 S 130th St, Chandler, AZ 85249',
    '1601 E Shannon St, Chandler, AZ 85225']

for index in range (len (addresses)) :
    cell = 'A' + str (index + 1)
    sheet [cell] = addresses [index]

wb.save ('tester.xlsx')
bashBedlam
  • 1,402
  • 1
  • 7
  • 11