Home > Mobile >  Pyxl combining two indices to find where they intersect in an excel sheet
Pyxl combining two indices to find where they intersect in an excel sheet

Time:08-08

inputmachine = input("Enter machine to be changed:") #this will be a dropdown
inputattribute = input("Enter attribute to be changed:")    #this will be a dropdown
newdata = input("Enter new data:") #this will be new cell value

def get_attributes(): 
    for col in sheet.iter_cols(min_row=1, max_col=10, max_row=1, min_col=2):
        for cell in col:
            if inputattribute == cell.value:
                attributeindex = cell
                return attributeindex

def get_machine():# this iterates all of my machines
    for row in sheet.iter_rows(min_row=2, max_col=1, max_row=20):
        for cell in row:
            if cell.value == inputmachine:
                machineindex = cell
                return machineindex



print(get_machine())
print(get_attributes())

This code finds the index of a row and the index of a column. I was basically thinking in x-y axes when I wanted to find the data. I am trying to write a program that allows the user to edit certain attributes of machines, so they need to be able to access first the machine which, in my thinking, was the y axis, and then the attribute, or x axis. From there, I collected the indices of the users choices, as shown below. This is being done in pyxl.

This code prints out this: <Cell 'Line 7 machines'.A2> <Cell 'Line 7 machines'.B1> How can I use these indexes to find where they will intersect? The intersection point is the spot that I want to access through an index and edit. For example, these indices would meet up at B2 on the excel sheet.

CodePudding user response:

You can get the row and column as separate values in your functions using

cell.column_letter 

and

cell.row

Then transpose the two coords to get either A1 or B2 (from the coords A2 and B1).

The official documentation for openpyxl is here https://openpyxl.readthedocs.io/en/stable/ and plenty of questions and answers here on stack overflow.

  • Related