I wrote my code using xlrd
package for extracting specific information from excel file with multiple sheets. I partial match a string and get the value in the next column, and sometimes, I get the values in the next row for the same column depending on the requirement.
The below code is a part of my code using xlrd
which works fine to pick value in the next column:
import xlrd
workbook = xlrd.open_workbook('sample_data.xlsx')
for sheet in workbook.sheets():
for rowidx in range(sheet.nrows):
row = sheet.row(rowidx)
row_val = sheet.row_values(rowidx)
for colidx, cell in enumerate(row):
if cell.value == "Student number":
sheet_name.append(sheet.name)
print("Sheet Name =", sheet.name)
customer_num.append(sheet.cell(rowidx,colidx+1).value)
print(cell.value + "=" , sheet.cell(rowidx,colidx+1).value)
But I now need to use openpyxl
instead of xlrd
to achieve this. It's a technical requirement. And I'm unable to find proper counterparts from the openpyxl
package. I'm pretty new to Python too.
It would be very helpful and time saving if someone who has good knowledge of both xlrd
and openpyxl
can help me on how to replicate my above code using openpyxl
. Thanks a lot.
for ws in wb:
for row in ws:
for cell in row:
if cell.value == "Student number":
print(sheet.title)
print("{0} = {1}".format(cell.value, cell.offset(column=1).value))