Currently I take an excel file and extract the data from a sheet with openpyxl, which gives me an object with several tuples from each excel row, these rows I save in an array (baseBitacora) to then take each "line" or tuple and add the date, then save it in another excel.
This is my code.
l_Alerta = openpyxl.load_workbook("D:/Plantilla.xlsm",read_only = False, keep_vba = True)
h_Alerta = l_Alerta.worksheets[0]
lBitacora = openpyxl.load_workbook("D:/Bitacora.xlsm",read_only = False, keep_vba = True)
hBitacora = lBitacora.worksheets[0]
baseBitacora = []
baseBitacora2 = []
a = []
h_bitacora = hora_Ejecucion.strftime("%d-%m-%Y %H-%M")
print("AGREGANDO DATOS a baseBitacora ")
for row in h_Alerta.values:
baseBitacora.append(row)
print("AGREGANDO HORA EN LA BITACORA") #Aca es donde me da error y no logro unir los datos del excel con la hora
for row in baseBitacora:
a = row
baseBitacora2 = a.append(h_bitacora)
print("baseBitacora2 ",baseBitacora2)
print("AGREGANDO DATOS EN LA HOJA BITACORA")
for row in baseBitacora2:
print(row)
hBitacora.append(row)
lBitacora.save("D:/bitacora.xlsm")
lBitacora.close()
l_Alerta.close()
As mateo says, it is impossible to add an item in a tuple. However, there are other alternatives:
Create a new tuple.
It is possible to do something similar to adding an element to a tuple, and that is to add another tuple to it. This will create a third tuple with the elements of the first tuple followed by those of the second:
Example:
Would produce (1,2,3,4,5,6).
You can also use the wonderful unpacking inside the tuple syntax:
What would produce:
Note that the original tuple, being immutable, remains unchanged. This means that if you store a tuple in a variable and add another tuple (obviously without using +=, which does the addition and stores it in the variable), the variable will not have changed:
would produce:
Something similar happens with lists, since during the addition, a new list is created. But you could one day find yourself with the scenario that the addition alters the original element (although this is not usual, it is possible).
You have to know how to differentiate when the code (at least in Python) creates something new or modifies the original. Remember that variables are actually names that you give to objects.
convert it to a list
The closest mutable object to a tuple to which elements can be added is a list. Simply convert the tuple to a list using the function
list
, append the data with append and then, if or if it should be a tuple, you can usetuple
to convert the list to a tuple again.It should produce: