如何在excel中放置正确的赋值

时间:2019-06-07 09:36:00

标签: python-3.x openpyxl

我正在尝试从sheet2分配sheet1值的单元格。该代码可以正常工作,但是存在逻辑错误,原因是获取错误的值。 香港专业教育学院尝试了不同的方法,但没有一个能给我正确的结果。

import openpyxl as xl

wb = xl.load_workbook('file.xlsx')
sheet1 = wb['Sheet1']
sheet2 = wb['Sheet2']

count = 0
for i in sheet2.iter_rows(values_only=True):
    count += 1
    for value in i:
        sheet1['H23'] = value
        sheet1['H24'] = value
        sheet1['H25'] = count


wb.save('file.xlsx')

例如,sheet2 A1,B1中的值是“ test1,test2” 而不是获取sheet1 ['H23'] = test1和sheet1 ['H24'] = test2 我正在获取sheet1 ['H23'] = test2和sheet1 ['H24'] = test2

我想要得到的是sheet1 ['H23'] = test1和sheet1 ['H24'] = test2

1 个答案:

答案 0 :(得分:0)

for i in sheet2.iter_rows(values_only=True):
    count += 1
    sheet1['H23'] = i[0]
    sheet1['H24'] = i[1]
    sheet1['H25'] = count

好的...这解决了...还是要谢谢