简体   繁体   中英

'tuple' object has no attribute 'value' openpyxl

I made a script that copies a range from one excel sheet to another, but I run into the following error when I run it.

Traceback (most recent call last):
  File "d:\Python\LCR_skema_opdater\202203-test\Skema\Moder\LCR_opdater_skema.py", line 31, in <module>
    cell2.value = cell1.value
AttributeError: 'tuple' object has no attribute 'value'

This is the script I wrote:

import openpyxl
import os

#Current path
path = os.path.dirname(os.path.abspath(__file__))

#Beregningsmodul navn
Beregningsmodul_moder_navn = "Beregning COREP LCR - MODER - 202202.xlsx"

#workbook_beregn path
beregn_path = path + "\\" + Beregningsmodul_moder_navn
workbook_beregn = openpyxl.load_workbook(beregn_path)

#Skema 72 navn
skema_72_navn ="C_72_00_a.xlsx"
#skema path
skema_72_path = path + "\\" + skema_72_navn
workbook_skema_72 = openpyxl.load_workbook(skema_72_path)

#Kopier til
wb_72C = workbook_beregn["72C"]['E8':'G54']

#kopier fra
C_72_00_a = workbook_skema_72["C_72_00_a"]['D9':'F55']

#Pair the rows
for row1,row2 in zip(C_72_00_a, workbook_beregn):
    #within the row pair, pair the cells
    for cell1, cell2 in zip(row1,row2):
        #assign the value of cell 1 to the destination cell 2 for each row
        cell2.value = cell1.value
#save document
workbook_beregn.save('destination.xlsx')
print("Finished")

I hope you can point me in the right direction.

#Update:

I found a solution and ended up defining a function to make it easier to replicate in the future:

#Copy from
wb_72C = workbook_beregn['72C']['E8':'G54']

#Copy to
C_72_00_a = workbook_skema_72['C_72_00_a']['D9':'F55']

#Define function
def workbook_copy(copy_from,copy_to):
    #Pair the rows
    for row1,row2 in zip(copy_from, copy_to):
        #within the row pair, pair the cells
        for cell1, cell2 in zip(row1,row2):
            #assign the value of cell 1 to the destination cell 2 for each row
            cell2.value = cell1.value

workbook_copy(C_72_00_a, wb_72C)

When the iteration is run, the return object is a tuple (combination of all values in the cells of the given row) for eg ('A', '123', 'CD', 125) will be result for a row with four columns

for copying values from source cell to target cell, you will need to iterate over the coordinates of the cell (row address and column address)

When you iterate over the rows, the code only understands the row and not each cell in the row. Hence the error, "AttributeError: 'tuple' object has no attribute 'value'"

I hope this helps

I found the issue I was iterating through the workbook instead of the tuple I had made.

wb_72C = workbook_beregn['72C']['E8':'G54']
C_72_00_a = workbook_skema_72['C_72_00_a']['D9':'F55']
#Pair the rows
for row1,row2 in zip(C_72_00_a, wb_72C):
    #within the row pair, pair the cells
    for cell1, cell2 in zip(row1,row2):
        #assign the value of cell 1 to the destination cell 2 for each row
        cell2.value = cell1.value

So I changed this line from:

for row1,row2 in zip(C_72_00_a, workbook_beregn):

to this line:

for row1,row2 in zip(C_72_00_a, wb_72C):

I hope this someone else in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM