使用Python和Openpyxl从特定单元格开始向现有的Excel文件追加多个列表的数量。

huangapple go评论62阅读模式
英文:

Appending no of lists to an existing excel file starting from a specific cells with Python, Openpyxl

问题

I'm getting no of list's in my_list variable through iterating a dictionary -> employee_data, it's not a list of list; it's coming through in a for loop one by one. Look into the code below...

print(my_list)

    ['A', 'B', 'C', 'D', 'E']
    ['B', 'C', 'B', 'B', 'B']
    ['C', 'C', 'C', 'C', 'C']
    ['A', 'B', 'C', 'D', 'E']
    ['D', 'D', 'D', 'D', 'D']
  1. I just want to add those list's items in Excel from cell between ws['C3':'H' + str(ws.max_row)] like this. Is it possible in a loop?

使用Python和Openpyxl从特定单元格开始向现有的Excel文件追加多个列表的数量。

And my code is:

for key, day_value in employee_data.items():
        attendance_array = []
        for val_key, get_value in day_value.items():
            attendance_array.append(get_value)
        my_list = attendance_array[n:]
        print(my_list)

        for rw in ws['C3':'H' + str(ws.max_row)]:
            for cell, val in zip(rw, my_list):
                cell.value = val
英文:

I'm getting no of list's in my_list variable trough iterating a dictionary -> employee_data, its not a list of list its coming trough in for loop one by one, look into code below...

print(my_list)

    ['A', 'B', 'C', 'D', 'E']
    ['B', 'C', 'B', 'B', 'B']
    ['C', 'C', 'C', 'C', 'C']
    ['A', 'B', 'C', 'D', 'E']
    ['D', 'D', 'D', 'D', 'D']
  1. I just want to add those list's item in excel from cell between ws['C3':'H' + str(ws.max_row)] like this is it possible in loop ?

使用Python和Openpyxl从特定单元格开始向现有的Excel文件追加多个列表的数量。

and my code is

for key,day_value in employee_data.items():
		attendance_array = []
		for val_key, get_value in day_value.items():
			attendance_array.append(get_value)
		my_list = attendance_array[n:]
        print(my_list)

        for rw in ws['C3':'H' + str(ws.max_row)]:
			for cell, val in zip(rw, my_list):
				cell.value = val

		

答案1

得分: 0

您的代码从employee_data字典中读取一个列表,预计是[ 'A', 'B', 'C', 'D', 'E'],然后将此列表应用于所有5行,因为您从C3到H7创建了一个范围,然后获得下一个列表,[ 'B', 'C', 'B', 'B', 'B'],然后执行相同的操作,以此类推处理其余的列表。不确定为什么范围需要扩展到H列,因为最后一列是G,但您需要在第一个循环中将范围设置为C3:G3,即仅处理第3行,然后在下一个循环中将范围设置为C4:G4,然后下一个循环设置为C5:G5等。

示例:

...
row = 3
for key, day_value in employee_data.items():
    attendance_array = []
    for val_key, get_value in day_value.items():
        attendance_array.append(get_value)
    my_list = attendance_array[n:]
    print(my_list)

    for rw in ws[f'C{row}:G{row}']:
        for cell, val in zip(rw, my_list):
            cell.value = val
    row += 1
英文:

Your code reads 1 list from employee_data dictionary so presumably ['A', 'B', 'C', 'D', 'E'] then applies this one list to all 5 rows since your create a range from C3 to H7 then you get the next list, ['B', 'C', 'B', 'B', 'B'] and do the same thing and so on for the rest of the lists. Not sure why your range needs to extend to column H since the last column is G but you need to set the range the first loop as C3:G3 i.e. row 3 only then on next loop set range to C4:G4, then next loop C5:G5 etc<br>
Example<br>

...
row = 3
for key, day_value in employee_data.items():
    attendance_array = []
    for val_key, get_value in day_value.items():
        attendance_array.append(get_value)
    my_list = attendance_array[n:]
    print(my_list)

    for rw in ws[f&#39;C{row}:G{row}&#39;]:
        for cell, val in zip(rw, my_list):
            cell.value = val
    row += 1

huangapple
  • 本文由 发表于 2023年2月9日 00:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定