我正在尝试将列表项存储在变量中,然后填充电子表格的单元格,直到列表中的项目用完为止(因此它可用于任何大小的列表)。我没有运气就尝试了各种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']
sheet['A1':] = addresses[0:]
您真正想要做的是将列表的每个元素添加到其自己的单元格中,如A1,A2,A3等。这样尝试,看看是否能为您提供所需的结果。
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')