Somewhat new to Python but this is a question about writing functions without repeating oneself.
Using openpyxl
I've created a document class and a workbook (wb
) with 3 sheets inside:
self.sheet1 = self.wb['sheet1']
self.sheet2 = self.wb['sheet2']
self.sheet3 = self.wb['sheet3']
I also have a function in this class that looks through rows of sheet3
for particular text.
def find_text_row_number(self, text):
text_row_number = set([])
for row in self.sheet3.iter_rows(self.sheet3.min_row,self.sheet3.max_row):
for cell in row:
if cell.value == text:
text_row_number.add(cell.row)
return sorted(text_row_number)
My question is: How can I call this function elsewhere in the program and have it search sheet2
, instead of sheet3
? Have tried looking into decorators and also using a default value (sheet=self.sheet3
), but this raises an error because self
has not yet been defined.
Is there maybe a more efficient way to write this particular function? Or some aspect of Python I'm missing that could help out here?