Dear Charlie,
Sorry for delaying in aswering you about this topic, this help me a lot. Thanks so much in advanced.
from openpyxl import load_workbook
libros = "test.xlsx"
trabajadores = "SONIA MARÍA MONTESDEOCA QUINTANA"
libro_in = load_workbook(libros)
hoja_in = libro_in['Enero']
dimension_excel = hoja_in.dimensions
mi_fila = hoja_in.min_row
ma_fila = hoja_in.max_row
mi_columna = hoja_in.min_column
ma_columna = hoja_in.max_column
for row in hoja_in.iter_rows(min_row=1, max_col=1, max_row=ma_fila):
for cell in row:
if cell.value == trabajadores:
print (row)
print(cell.value)
This is console output:
*** Remote Interpreter Reinitialized ***
(<Cell 'Enero'.A7>,)
SONIA MARÍA MONTESDEOCA QUINTANA
(<Cell 'Enero'.A53>,)
SONIA MARÍA MONTESDEOCA QUINTANA
Next step is use these rows to look up another values from columna 4 until ma_columna. Now I have a loop with following code:
from openpyxl import load_workbook
libros = "Z:\\Informática\\Desarrollo\\alcampo\\Horas Personal Alcampo Telde PGC Salado 2.021.xlsx"
trabajadores = "SONIA MARÍA MONTESDEOCA QUINTANA"
libro_in = load_workbook(libros)
hoja_in = libro_in['Enero']
dimension_excel = hoja_in.dimensions
mi_fila = hoja_in.min_row
ma_fila = hoja_in.max_row
mi_columna = hoja_in.min_column
ma_columna = hoja_in.max_column
for row in hoja_in.iter_rows(min_row=1, max_col=1, max_row=ma_fila):
for cell in row:
if cell.value == trabajadores:
print (row)
print(cell.value)
for objeto_columna in range (3,ma_columna-2):
horas_merchand = hoja_in.cell(row=cell,column=objeto_columna).value
print ("workes name: ", cell.value, " make these working hours: ", horas_merchand)
But as I guest python gives me an error, trying to access to access to read a cell value. Accoding to doc there is two ways to access value with row:
Traceback (most recent call last):
File "<module1>", line 33, in <module>
File "C:\Users\tpv09.TPVCAN\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\worksheet.py", line 237, in cell
if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'Cell' and 'int'
In the example, we write two values to two cells.
sheet['A1'] = 1
Here, we assing a numerical value to the A1 cell.
sheet.cell(row=2, column=2).value = 2
I use second one but it does not work with loop (for), any idea how to solve this?
Thanks so much in advanced.
P.S. Im very beginner at dev in python ;-)