Using xlrd, how do you return the value of a cell next to another?

Viewed 23

I've got to print the value of a cell next to another in a a lot of .xls sheets. I have to find "part number" and the print the number that's next to it. The cell is on the right. It's gotta repeat until it's done with all the folders.

I got it working, but I can't figure out how to print the value of the cell next to the cell I found.

import xlrd
import os
os.chdir(r"C:\Users\sdiaz\Desktop\Active")
Excelsheet = "1.xls"
wb = xlrd.open_workbook(Excelsheet)
ws = wb.sheet_by_index(0)

def find_parts(filename):
    for rowidx in range(ws.nrows):
        row = ws.row(rowidx)
        for colidx, cell in enumerate(row):
            if cell.value == "part number" or "Part Number" or "PART NUMBER":
                return ws.cell(rowidx,colidx+1).value
for root, _, files in os.walk(r'C:\Users\sdiaz\Desktop\Active'):
    for file in files:
        if file.endswith('.xls'):
            print(find_parts(file))

and this is what I get

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [333], in <cell line: 1>()
      2 for file in files:
      3     if file.endswith('.xls'):
----> 4         print(find_parts(file))

Input In [332], in find_parts(filename)
     11 for colidx, cell in enumerate(row):
     12     if cell.value == "part number" or "Part Number" or "PART NUMBER":
---> 13         return ws.cell(rowidx,colidx+1).value

File ~\Anaconda3\lib\site-packages\xlrd\sheet.py:413, in Sheet.cell(self, rowx, colx)
    410 else:
    411     xfx = None
    412 return Cell(
--> 413     self._cell_types[rowx][colx],
    414     self._cell_values[rowx][colx],
    415     xfx,
    416 )

IndexError: array index out of range
0 Answers
Related