2

I have a Google Spreadsheet, column1 contains test_name, and column two contains test result (pass or fail). I want to search the test_name (string) in spreadsheet and if it matches, fetch the row, column of the string and update the result in column2.

try:
    worksheet = sh.worksheet(sheetName)
    print("got access to worksheet",worksheet)
except Exception as Ex: 
    print(Ex)

with open(PATH) as f:
    for line in f:
        if line.startswith("PASS") or line.startswith("FAIL") or line.startswith("SKIP") or line.startswith('INELIGIBLE') or line.startswith('ERROR'):
            print("line",line)
            index=line.index("")
            result=line[0:index]
            tname=line[index+1:]
            print("result",result)
            print(result+" "+ tname)
            list1.append(tname)
            print("*******************************")
            try:
                cell=sh.find(tname)
                row=cell.row
                column=cell.col
                print("row",row)
                print("col",col)
                print("cell",cell)
            except Exception as Ex:
                print(Ex)

the cell.find(tname) is showing error and not able to fetch case insensitive (or spacing) string.:

'Spreadsheet' object has no attribute 'cell'

I have fetch the exact row,col of the string.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Manish
  • 21
  • 3
  • You need to use `worksheet.find(tname)`. `sh` in your case is an instance of `Spreadsheet` which does not have a `cell` attribute indeed. This is why you're getting an error. – Burnash Sep 28 '18 at 07:28

1 Answers1

0

You can try below code:

from openpyxl import load_workbook

wb = load_workbook('~/Documents/test_sheet.xlsx')
sheet = wb.get_sheet_by_name('Sheet 1')
sheet = wb.active

def get_row_column_values(workbook,sheet, col1, col2, row_num_to_startwith, string_to_match):
     passed='PASS'
     failed='FAIL'

     for i in range(row_num_to_startwith, sheet.max_column+1):
     if sheet[col1+str(i)].value:

        if sheet[col1+str(i)].value.strip().lower() == str(string_to_match.lower()):
            sheet[col2+str(i)] = passed
        else:
            sheet[col2+str(i)] = failed

     workbook.save('~Documents/another_copy.xlsx')


get_row_column_values(wb, sheet, 'A', 'B', 3, 'PQR')

Output can be seen as:

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Pranay
  • 17
  • 2