将值从一个表复制到另一个表时出错

时间:2017-12-10 21:36:17

标签: python excel openpyxl

我正在尝试复制某些单元格中的值,但它给了我这个错误,即使不使用def单元格(x,y)我也尝试过但仍然是同样的错误。

这是错误:

learn_tar.cell(row=learn_tar, column=1).value = sheet.cell(row=learn_tar, column=1).value
AttributeError: 'int' object has no attribute 'cell'

来源:

import openpyxl 

def cell(x,y):
   cell = sheet.cell(row=x,column=y).value
   return cell; 

def percentage(percent, whole):
  return int((percent * whole) / 100.0);


ex = openpyxl.load_workbook("Final_excel2.xlsx")
sheet = ex.get_sheet_by_name('Sheet1')

num = [0,0,0]
per = [0,0,0]

for row in range(2,4798):

    if cell(row,1) == '1: Progression':
        num[0] = num[0] + 1
    elif cell(row,1) == '2: Incidence':
        num[1] = num[1] + 1
    elif cell(row,1) == '3: Non-exposed control group':
        num[2] = num[2] + 1

    for column in range(2,49):
       #doing stuff

per[0] = percentage(70,num[0])
per[1] = percentage(70,num[1])
per[2] = percentage(70,num[2])

learn_att = ex.create_sheet('Learn-Att',2)
learn_tar = ex.create_sheet('Learn-Tar',3)

test_att = ex.create_sheet('Test-Att',4)
test_tar = ex.create_sheet('Test-Tar',5)

learn_att = 1
learn_tar = 1

test_att = 1
test_tar = 1



for row in range(2,4798):

    if row<=1391:
        if row<=974:                
            learn_tar.cell(row=learn_tar, column=1).value = cell(row,1)
            learn_att+= 1
            learn_tar+= 1
        else:
            test_tar.cell(row = test_tar,column = 1).value = cell(row,1)
            test_att+= 1
            test_tar+= 1

    for column in range(2,49):

        if row<=1391:
            if row<=974:                
                learn_att.cell(row = learn_att,column = column - 1).value = cell(row,column)
            else:
                test_att.cell(row = test_att,column = column - 1).value = cell(row,column)

1 个答案:

答案 0 :(得分:2)

您使用learn_tar覆盖1

learn_tar = ex.create_sheet('Learn-Tar',3)
...
learn_tar = 1

卸下:

learn_tar = 1

learn_tar+= 1

来自你的代码。